9

I tried some code but getting an exception.

The Exception that I'm getting: java.lang.IllegalArgumentException: Invalid document reference. Document references must have an even number of segments, but Users has 1

I searched for it, according to this, Document references must have an even number of segments like: Collection - document - Collection - document - Collection - document

Query for getting data from firestore:

String getIsNewUSer;
Firestore.instance.collection('Users').document(uid).get().then((DocumentSnapshot document){
       print("document_build:$document");
        setState(() {
         getIsNewUSer=document['IsNewUser'];
         print("getIsNewUSe:$getIsNewUSer");
        });
             
    });

Query for Updating data to the firestore:

Firestore.instance
            .collection('Users')
            .document(uid)
            .updateData({
              "IsNewUser":"1"
            }).then((result){
              print("new USer true");
            }).catchError((onError){
             print("onError");
            });

These code line at I'm getting above Exception.

initState:

 void initState()  {
    super.initState();
    this.uid = '';

FirebaseAuth.instance.currentUser().then((val){
      setState(() {
      this.uid= val.uid; 
       print("uid_init: $uid");
      });
   });

}
krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88
Shruti Ramnandan Sharma
  • 4,027
  • 13
  • 44
  • 76

3 Answers3

15

Null safe code:

  • Get data:

    var collection = FirebaseFirestore.instance.collection('collection');
    var docSnapshot = await collection.doc('doc_id').get();
    Map<String, dynamic>? data = docSnapshot.data();
    
  • Set data:

    var collection = FirebaseFirestore.instance.collection('collection');
    collection.add(someData);
    
  • Update data:

    var collection = FirebaseFirestore.instance.collection('collection');
    collection 
        .doc('foo_id') // <-- Doc ID where data should be updated.
        .update(newData);
    
  • Delete data:

    var collection = FirebaseFirestore.instance.collection('collection');
    collection 
        .doc('some_id') // <-- Doc ID to be deleted. 
        .delete();
    
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
6

Replace this part in your queries:

Firestore.instance.collection('Users').document(uid)

with

Firestore.instance.document('Users/$uid')

Collection - document - Collection - document - Collection - document

Basically you already had the answer.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ryosuke
  • 3,592
  • 1
  • 11
  • 28
  • 1
    Hello @Shruti Ramnandan Sharma! If this answer worked out for you, would you mind accepting it? That way tt will have more visibility on the community and other users with the same doubt may benefit from it! – Pablo Almécija Rodríguez Sep 25 '19 at 14:30
-1

It is possible that FirebaseAuth.instance.currentUser() future didn't complete and populte this.uid. So this.uid == '' (empty string). So Firestore is throwing errror as you are trying to updated document at Users which is a collection.

You can validate this by printing this.uid before the update statement.

One way is to use helper method to update

Future<void> update(Map data) async {
    final user = await FirebaseAuth.instance.currentUser();
    return Firestore.instance.collection('Users').document(user.uid).updateData(data);
}

Then you can use helpr method as update({isNewUser: "1"}).then((r) {...})....

You can follow same approach for fetching the document as well.

Chenna Reddy
  • 2,241
  • 18
  • 17
  • I think it would, Firestore.instance.collection('Users').document(uid) should be replacable to Firestore.instance.document('Users/$uid'). I guess the main issue here is uid is not set (so default value '') by the time query is fired. – Chenna Reddy Sep 25 '19 at 17:53
  • Have you ever tried it? It doen't work that way. Even if it is logically correct, but firestore requires you to write the document path with even number of parts. – Ryosuke Sep 25 '19 at 17:57
  • I use this pattern heavily. e.g. https://github.com/yagnyam-in/proxy-flutter/blob/master/lib/db/firestore_utils.dart. This app is on playstore and appstore. – Chenna Reddy Sep 25 '19 at 18:02
  • you can not use "then" after "update". It returns void – Stellar Creed Sep 07 '22 at 00:48