When should i use streams vs just accessing the cloud firestore once in flutter?

1,424

Solution 1

The technical difference is that get only gets the document once, while the stream will get the current data straight away, and then continue to listen for changes.

I typically use a stream (or its underlying onSnapshot()) when I display the data straight into the UI, because that means the UI updates whenever the data changes. This is one of the really cool Firebase features, because it makes your UI reactive to the data changes. I use get() for things that I only need once, such as configuration data (although it's also very cool if you use a stream for that), client-side joins, etc.

Solution 2

It all really comes down to whether you want the data to reload every time something changes in your database.

  • If you want it to update as it changes in your database, use a Stream (most likely with a StreamBuilder)

  • If you want it only to update when you reload the screen, use get() like you are in your example

Share:
1,424
Potato
Author by

Potato

Updated on December 11, 2022

Comments

  • Potato
    Potato over 1 year

    I want to create a groups functionality for my app, so far when i set up the profile page of each user, i've used something like this :

    DocumentReference documentReference =
                   _firestore.collection("users").document("$email");
                   await documentReference.get().then((DocumentSnapshot datasnapshot) {
                     if (datasnapshot.exists) {
                       displayName=datasnapshot.data['displayName'].toString();
                       bio=datasnapshot.data['bio'].toString();
                       print(bio);
                     }
                     else {
                       print("No such user");
                     }
    

    This works but im thinking if i want to create groups and record the changes that different users may make then i should probably use a stream is that correct? Generally i am unsure when to use which if anyone could provide some insight?

  • Potato
    Potato almost 5 years
    Okay that makes sense, lets say a page didnt have a button to set the state or make changes to the values then a stream would be better but if a page did have a button that changed the state or edited values on pressed, then using get may be better in this case? For instance, if i edit or load a profile page im currently using get because the data is not likely to change unless they specificly go and change it and click the button, but say a messaging system would probably use streams, correct?
  • Potato
    Potato almost 5 years
    Okay thanks, and which of the two will require more power and may possibly slow down my app if used unnecessarily?
  • Frank van Puffelen
    Frank van Puffelen almost 5 years
    Definitely a "yes" on that last example. The other ones are more opinionated. I for example tend to see reactive systems in many more use-cases than most people. So judge for yourself what is reasonable to you there, and don't be dogmatic about it if your views change over time.
  • Tadas Petra
    Tadas Petra almost 5 years
    StreamBuilder will definitely require more power, since its going to be refreshing and communicating with the database every time it changes. Using just a get will call it once, and you're done. However, unless your app is humongous, using a Stream should not slow it down that noticeably. And if it does, then there are better implementations, for example only stream 10 things, and then if the user scrolls stream 10 more, etc.
  • alexlipa
    alexlipa almost 3 years
    I would like to use the power of streams to not manually manage updates/reloads of documents (e.g. if I use get, then I need to trigger a pull and notifyListeners every time). Is there a way to tell a stream "give me current stream status and do not look for changes until the page is on top of the stack"?