Vincenzo
Vincenzo

Reputation: 6388

Is a Stream<List<Object>> from Firebase snapshot returning the entire node at every change in the node? Flutter

I'm starting learning Flutter as I want to port my iOS app to Flutter to also run on Android. I use Firebase real time database for the back-end and I saw that the firebase_database package should suit my needs, allowing me to use the db I already have. I'm also learning to use the BLoC pattern but all the bloc tutorials I found are for Firestore and all the Event/State/Bloc/Repository/Streaming is still quite confusing to me right now. What should be the right implementation of the bloc pattern?

To a basic level I do understand it and I like it very much, but thinking of a way to implement it for my needs is resulting a bit overwhelming.

Following this diagram the 6 connections data flow should be: enter image description here

  1. (UI -> BLoC) An event will be sent to the bloc.
  2. (BLoC -> Repository) The Bloc maps the event to a Repository method.
  3. (Repository -> Database) The Repository methods are database Create/Update/Delete methods.
  4. (Database -> Repository) The Repository database Read method gets database Data.
  5. (Repository -> BLoC) The return from Repository database Read method takes the Data into BLoC.
  6. (BLoC -> UI) The BlocProvider uses the Data to rebuild the UI.

Now what I'm not sure I figured out is the data going back to the UI.

At step 2 if I map the event to a method that returns the data as a Stream<List<Object>> then via a BlocProvider at step 6 the UI(stateless widget) gets rebuilt accordingly. Now, as I see it, that would be a lot of unnecessary repeating data downloads if for any change, the whole node gets downloaded, also Google's bills you on downloads.

In the Firebase iOS SDK you can attach observers to a node, so you just get a snapshot with the .childAdded/.childRomovedetc etc, and use it to modify your UI.

To avoid useless downloads and keep my account safe, I'd rather make a query on a node with the keepSynced bool set to true ( https://pub.dev/documentation/firebase_database/latest/firebase_database/Query-class.html ). This should (as the iOS SDK) return at first firing, a snapshot with the whole node and than snapshots with just new/updated/delete when something changes right?

To implement this way instead, should I use a List<Object> that I update manually when getting the snapshot and a stateful widget? This is actually how my iOS app works at the moment. Thank you very much for the help and sorry for the long question, making the switch from Swift is taking it's time.

Upvotes: 0

Views: 502

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317657

Firebase listeners only transfer the minimal amount of data that actually changed at the node being listened to. It does NOT transfer the entire node and all of its children each time anything changes. So, it's not as expensive as you're imagining it to be.

Upvotes: 2

Related Questions