Firestore snapshot used in flutter StreamBuilder never gets updated when data changes

745

you need to create an index utilizing the where and orderBy fields:

https://firebase.google.com/docs/firestore/query-data/indexing

{
  "collectionGroup": "XXX",
  "queryScope": "COLLECTION",
  "fields": [
    {
      "fieldPath": "userId",
      "order": "ASCENDING"
    },
    {
      "fieldPath": "YYY",
      "order": "DESCENDING"
    }
  ]
},
Share:
745
shaimo
Author by

shaimo

Updated on December 18, 2022

Comments

  • shaimo
    shaimo over 1 year

    I'm a noob with flutter, trying to work with some examples. I created a widget that uses a StreamBuilder to pull some query from Firebase. It works great, but it never updates the UI when the data changes. I tried reading about it and from all I can see this should work out of the box. Is there some step I'm missing on the Firebase side to enable the changes to be pushed? Or does the SteamBuilder somehow needs to be configured so it pulls for data every once in a while? The code is basic, just copy and paste from some examples:

    @override
      Widget build(BuildContext context) {
        return StreamBuilder(
          stream: Firestore.instance
            .collection("XXX").orderBy('YYY')
            .where("userID", isEqualTo: userID)
            .snapshots(),
          builder: (BuildContext context, AsyncSnapshot snapshot) {
              if (!snapshot.hasData) {
                return Center(
                  child: CircularProgressIndicator(
                    valueColor: new AlwaysStoppedAnimation<Color>(
                      Color.fromRGBO(212, 20, 15, 1.0),
                    ),
                  ),
                );
              } else {
                // Return some list view using the data in snapshot.data.documents
              }
            }
          );
        }
    
    

    [Update]: seems like my data is not updated even when I change the query. I guess it's using some locally cached data and is not fetching it again from the server.

    [Update 2]: when I use a query that resolves to no results (e.g. non-existing collection) the spinning wheel just keeps going. When running again the correct query it goes back to the same old results, not reflecting any changes in the Firebase cloud.

    [Update 3]: the problem seems to do with my stream containing orderBy. As soon as I remove that everything works fine. Is that a bug??

    • Frank van Puffelen
      Frank van Puffelen about 4 years
      "Is there some step I'm missing on the Firebase side to enable the changes to be pushed?" At first glance your code looks correct. snapshots() returns a stream that updates when the data changes, and no additional configuration is needed. You might want to listen to the stream somewhere else in your code, to see if that shows you the updates. If it does, you at least know the problem is somehwere in the rendering.
    • shaimo
      shaimo about 4 years
      @FrankvanPuffelen I added some debug messages in the build function, just before rendering the widgets - the snapshot does show the old info and not what's currently in the database. I even tried restarting the app, not with hot reload, but still the same old data. It doesn't seem like it's getting any new data at all (even with my 2nd update above)...
    • Frank van Puffelen
      Frank van Puffelen about 4 years
      Can you extract the entire Firestore code from the build method, use listen to observe the stream, and then just print the data or something. If you can reproduce the problem with that, update your question to (only) show that code and its output. It's easier to help once we can rule out the rendering (although I have no idea straight away that the problem might be).
    • shaimo
      shaimo about 4 years
      @FrankvanPuffelen please see my [update 3] - the problem seems to be with the orderBy being part of the stream. If I remove it then everything works just fine. Is that a bug I should report or am I not using it correctly?
    • Frank van Puffelen
      Frank van Puffelen about 4 years
      Without the test I asked for in my previous comments, I won't be able to help further.
  • shaimo
    shaimo about 4 years
    I also thought this was the issue after finding this: github.com/flutter/flutter/issues/15928. But it doesn't seem to work even with a single orderBy. When I add an index for it it doesn't really do anything because that index is already automatically created. I also tried two orderBy and added a composite index but am still getting the old results... If it was an index error shouldn't I be seeing an error message somewhere?
  • shaimo
    shaimo about 4 years
    ok, figured it out. It needs to be a composite index of the where and orderBy fields. @blaneyneil if you care to add this to your answer I will mark it as the actual answer. Thanks
  • blaneyneil
    blaneyneil about 4 years
    just updated to show a firestore.indexes.json example