Unexpected behaviour of onChildAdded stream in firebase_database

44
July 11, 2019, at 7:10 PM

I've recently started learning Flutter and the FlutterFire plugins. Yesterday I was working with the firebase_database plugin which allows adding Firebase Realtime Database to Flutter. While trying out a simple read on the database I noticed some strange behavior in one of the Streams provided by firebase_database which is onChildAdded.

So my problem is that when I use the onChildAdded stream with a StreamBuilder it only returns a single latest child. When I used to work with Firebase Database in Java the onChildAdded method was called for each child of a DatabaseReference instead of just the latest child. (Assuming that onChildAdded provides the same behavior in both Java and Dart)

I should also mention that when I use the onValue stream everything works fine and I get all the children of my DatabaseReference.

This is how my Firebase Database looks:

Code using onChildAdded

Widget _getBody(BuildContext context) {
    final DatabaseReference databaseRef = FirebaseDatabase.instance.reference();
    return StreamBuilder(
      stream: databaseRef.child("notes").child('android').onChildAdded,
      builder: (BuildContext context, AsyncSnapshot<Event> snapshot) {
        if (snapshot.hasData) {
          Map<dynamic, dynamic> notes = snapshot.data.snapshot.value;
          notes.forEach(
              (key, value) {
                print(notes[key]);
              }
          );
        }
        return Container(); //Just a blank widget because builder has to return a widget
      },
    );
  }

Output for onChildAdded:

Code using onValue

Widget _getBody(BuildContext context) {
    final DatabaseReference databaseRef = FirebaseDatabase.instance.reference();
    return StreamBuilder(
      stream: databaseRef.child("notes").child('android').onValue,
      builder: (BuildContext context, AsyncSnapshot<Event> snapshot) {
        if (snapshot.hasData) {
          List<dynamic> notes = snapshot.data.snapshot.value;
          notes.forEach(
              (item) {
                print("$item \n");
              }
          );
        }
      return Container(); //Just a placeholder because builder has to return a widget
    },
  );
}

Output for onValue:

So I was hoping for a way where it's possible to get all the children using the onChildAdded stream. Any help is appreciated!

READ ALSO
How to use SQLite in android fragments?

How to use SQLite in android fragments?

I am an aspiring android developerI wanted to make a simple project with SQLtite support, but I ran into the problem of SQLite whith Fragment

27
can we make custom notification show inside the android studio on the top of the screen instead the status bar?

can we make custom notification show inside the android studio on the top of the screen instead the status bar?

I need to create a list of custom notifications each time just one notification will appear on the screen but I need those notifications to show inside the APP instead of the status bar I am using android 9 so we need to use CHNNEL_IDIn case we can do that, will it be considered...

49
IllegalStateException when adding Fragment from Backstack

IllegalStateException when adding Fragment from Backstack

From time to time I get the following crash report:

37
Layer list gets cropped to drawable

Layer list gets cropped to drawable

This is the desired drawable which I am trying to build

49