Issue
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
The error message:
Invalid document reference. Document references must have an even number of segments, but userFavorites/FRbYxmNpSBcda6XOrQUjukvFvVb2/q7eLxtZfhG3g6Pd1bYY4 has 3
is telling you that you built a path to a document:
userFavorites/FRbYxmNpSBcda6XOrQUjukvFvVb2/q7eLxtZfhG3g6Pd1bYY4
which doesn't look like a document at all, since it has three path segments:
- userFavorites
- FRbYxmNpSBcda6XOrQUjukvFvVb2
- q7eLxtZfhG3g6Pd1bYY4
This is the line of code that built your path:
Firestore.instance.collection("userFavorites/$userId").document(id)
Since we can't see your data, it's hard to tell what you actually meant to do here. But in any event, Firestore is taking "userFavorites" to be the name of a top level collection, "FRbYxmNpSBcda6XOrQUjukvFvVb2" is the name of a document in that collection, and "q7eLxtZfhG3g6Pd1bYY4" is taken to mean a subcollection under that document. If you meant something else, you'll have to figure out how to build the path to that document to query it.
Answered By - Doug Stevenson
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.