简体   繁体   中英

Flutter: PlatformException(error, Invalid document reference. Document references must have an even number of segments, but <x> has 1, null)

I have the following in my app:

stream: Firestore.instance.collection('stores').document(currentUserUID).snapshots(),
  builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
     if (snapshot.hasData) {
       return ListView.builder(
         //do some stuff here with the data
       } else {
         return LoadingAnimation();
       }
     }
   }
 }

Pretty standard, but I am getting the error listed above.

I have vertified that currentUserID populates with the correct data before this stream is called.

In testing I have found that this error is perhaps likely caused by the fact that at runtime, "if (snapshot.hasData) {" shows me that there is no data in the snapshot yet. If I do anything at all, like minimise the app and refocus it again, all the data loads. So it simply just needs a redraw.

Is there a way to do an "await" or something on this? There must be a simple fix for this problem, seems to be a simple issue, but I just can't find a solution in any of the other threads about this.

Anyone have any ideas on how I can either do an await on this stream, or force it to update when it receives data (I kinda thought that was the whole point of a stream, but this examples proves that it doesn't do this).

If the error message actually refers to this code, that means currentUserUID is probably an empty string. This is a very common mistake - if you do a Google search for the error message, you will see many others making the same mistake.

Print the value of currentUserUID just before you use it in order to verify that it contains the value that you expect.

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