简体   繁体   中英

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

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

}

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM