Flutter firebase firestore: 'path.isNotEmpty': a document path must be a non-empty string)
Asked Answered
S

2

6

im trying to make chat app for my flutter application. But this error is always come up everytime i try to click to chat. The error Can someone tell me what is wrong with my code.

readLocal() async {
prefs = await SharedPreferences.getInstance();
id = prefs.getString('id') ?? '';
if (id.hashCode <= peerId.hashCode) {
  groupChatId = '$id-$peerId';
} else {
  groupChatId = '$peerId-$id';
}
FirebaseFirestore.instance
    .collection('users')
    .doc(id)
    .update({'chattingWith': peerId});

setState(() {});

}

Syncretize answered 24/5, 2021 at 1:51 Comment(0)
B
4

If looks like your id variable is an empty string, which isn't a valid document ID to pass into doc().

Given how you initialize id:

id = prefs.getString('id') ?? '';

It seems that prefs.getString('id') returns null. You'll want to figure out what you want to do when that happens, but a simple way is to check with:

id = prefs.getString('id');
if (!id.isEmpty) {
  if (id.hashCode <= peerId.hashCode) {
    groupChatId = '$id-$peerId';
  } else {
    groupChatId = '$peerId-$id';
  }
  FirebaseFirestore.instance
    .collection('users')
    .doc(id)
    .update({'chattingWith': peerId});

  setState(() {});
}
Blinding answered 24/5, 2021 at 3:9 Comment(4)
i try like u said. but the error still the same. it said that id.isEmpty is return nullSyncretize
Can you show the exact error message?Blinding
Exception has occurred. NoSuchMethodError (NoSuchMethodError: The getter 'isEmpty' was called on null. Receiver: null Tried calling: isEmpty)Syncretize
Hmmm... that should've failed the same before. But if id can be null too, you can check for that with if (id != null && !id.isEmpty) { or if (!id?.isEmpty) {.Blinding
W
0

Instead of '', I just passed '1' . Issue has been solved. Initially it has to be non empty. It can take any non empty value.

Wellnigh answered 19/1, 2023 at 7:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.