Search code examples
fluttergoogle-cloud-firestoreasync-awaitstream-builder

StreamBuilder showing data only on hot reload in flutter


I am facing a problem with Streambuilder. The workflow of my app is like there is a list of items when I press on add to favorites it adds the item to firestore. Then I am fetching data from firestore using stream and showing it on another screen using stream builder. The problem is that the stream builder is empty when I hot reload the app the data shows perfectly. this is the part where I am calling stream method this is streambuilder Thanks.


Solution

  • Try removing the extra variable resultStream and directly use the function DatabaseManager.getUserCurrency() in your StreamBuilder like this:

    StreamBuilder(
       stream: DatabaseManager.getUserCurrency(context),
       builder: (context, snapshot){...}
    )
    

    if this does not fix the issue, check your getUserCurrency() function for errors. This is an example for getting a Stream from Firestore:

    Stream<UserData> getUserData(String uid) {
      try {
        return userCollection
            .doc(uid)
            .snapshots()
            .map((event) => event?.data() != null ? UserData.fromMap(event.data()) : null);
      } catch (e) {
        print(e);
        return null;
      }
    }
    

    Additionally you normally don’t have to await a Stream, which might cause the issue in your case, because you are calling the async function in your initState.