0

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).

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Bisclavret
  • 1,327
  • 9
  • 37
  • 65
  • can you add more details about, how you get value of currentUserUID? – Viren V Varasadiya May 17 '20 at 09:00
  • In a previous screen I grab it out of shared preferences. None of the firebase methods I have tried for getting the uid ever work. So I take matters into my own hands, when you first sign up I store the uid as a shared pref. I get this value before I even launch this screen. When I start this screen the value I get for currentUserUID is jOID51blWsRA9g6Tsc3bIZzbXEa2 which then passes to the stream. – Bisclavret May 17 '20 at 09:18
  • I'm not fluent with Flutter, but you could check [this answer](https://stackoverflow.com/a/53817507/10810527) and you will see a stream with an `await` operation that could be helpful. I have also found [this post](https://stackoverflow.com/a/46639620/10810527) and [this other one](https://stackoverflow.com/a/50653727/10810527) that may be useful in this case. – Ajordat May 18 '20 at 15:47

1 Answers1

2

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.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441