0

This is the imported json for the firebase realTime DB:

    "FAT6To4D48YRzhO3fz4VcEFIapZ2" : {
      "groups" : {
        "-MNKG-FRw9weSrPWQjOc" : true,
        "-MNKG-FRw9weSrPWQjO1" : true
      }
    }
  }

This is the code for fatching the information into flutter:

    var user = _auth.currentUser.uid;
    var fb = FirebaseDatabase.instance.reference();
    List<Group> list = List();
    var snapshot = await fb.child("userGroups").child(user).child("groups").once();
    LinkedHashMap groups = snapshot.value;
      groups.forEach((key, val) async {
          var snaps = await fb.child('groups/$key').once();
          LinkedHashMap val = snaps.value;
          list.add(Group(
            name: val['name'],
            description: val['description'],
            groupId: key,
          ));
        });
      return list;
  }

Is this the correct way to do it? For now - the forEach loop is not iterating

Orly
  • 101
  • 6
  • You'll want to check what `snapshot.value` returns. Also: there is no need to do `await fb.child('groups/$key').once()`, as you already have that value in `val`. – Frank van Puffelen Feb 18 '21 at 15:52
  • The snapshot.value shows – Orly Feb 19 '21 at 09:09
  • ```Map (2 items) 0:"-MNKG-FRw9weSrPWQjOc" -> true 1:"-MNKG-FRw9weSrPWQjO1" -> true``` – Orly Feb 19 '21 at 09:19
  • The question is also is the fact that each group that we get, we need to go to the DB to fetch its info - looks like "expensive" approach – Orly Feb 19 '21 at 09:23
  • That question I can more easily answer: the so-called client-side join is not nearly as expensive as you expect, since Firebase pipelines those requests over its existing web socket connection. See https://stackoverflow.com/questions/35931526/speed-up-fetching-posts-for-my-social-network-app-by-using-query-instead-of-obse/35932786#35932786 – Frank van Puffelen Feb 19 '21 at 15:28

0 Answers0