'Firestore: Invalid document reference. Document references must have an even number of segments,
I have a futter app and want to add items favorite by the user in a separate collection "userFavorites" which will store all favorite items depending on the current user uid by doing so:
Future getCurrentUser() async {
final FirebaseUser user = await FirebaseAuth.instance.currentUser();
final uid = user.uid;
return uid.toString();
}
Future<void> toggleFavoriteStatus() async{
var userId = await getCurrentUser();
final oldStatus = isFavorite;
isFavorite = !isFavorite;
notifyListeners();
try{
await Firestore.instance.collection("userFavorites/$userId").document(id).updateData({
'isFavorite': isFavorite,
});
}catch(error){
_setFavValue(oldStatus);
}
}
But I receive this error when I try to favorite any item:
Invalid document reference. Document references must have an even number of segments, but userFavorites/FRbYxmNpSBcda6XOrQUjukvFvVb2/q7eLxtZfhG3g6Pd1bYY4 has 3
E/MethodChannel#plugins.flutter.io/cloud_firestore(14551): at com.google.firebase.firestore.DocumentReference.forPath(com.google.firebase:firebase-firestore@@21.3.0:80)
Solution 1:[1]
I ran into this problem using a doc reference & instead of using [document_reference].path I accessed it using the ID: [document_reference].id and that solved it for me.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Binyamin Robbins |
