Flutter firebase firestore: ‘path.isNotEmpty’: a document path must be a non-empty string)

Total
1
Shares

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(() {});

}


Solution

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(() {});
}
Leave a Reply

Your email address will not be published. Required fields are marked *