2

I made what I believe were the correct changes, however, now I'm getting an error that says I can't use forEach here because it may return null. I can't force it by using '!' because then I get another error about not being able to because the method 'forEach' isn't defined for the type 'Object'.

void loadStudentList(){
    // function that loads all students from firebase database and display them in list view
    FirebaseDatabase.instance.ref("students").once()
        .then((databaseEvent) {
      print("Successfully loaded the data");
      print(databaseEvent);
      print("Key:");
      print(databaseEvent.snapshot.key);
      print("value:");
      print(databaseEvent.snapshot.value);
      print("Iterating the value map");
      var studentTmpList = [];
      databaseEvent.snapshot.value!.forEach((k, v) {
        print(k);
        print(v);
        studentTmpList.add(v);
      });
      print("Final student list");
      print(studentTmpList);
      studentList = studentTmpList;
      setState(() {

      });
    }).catchError((error) {
      print("Failed to load the data");
      print(error);
    });
  }
0

1 Answer 1

1

What you are naming datasnapshot is actually a databaseEvent and should really be named as such for clarity. The event has a snapshot and the snapshot has a key and a value. So, to get the key you should be able to use datasnapshot.snapshot.key. Similarly, the value will be datasnapshot.snapshot.value.

You really should read this doc... https://firebase.flutter.dev/docs/database/usage/

UPDATE:

Based on the comment below, in your revised code change

databaseEvent.snapshot.value!.forEach((k, v) {...

to

final snapshotValue = databaseEvent.snapshot.value! as Map<dynamic, dynamic>;     
snapshotValue.forEach((k, v) {...
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. I've taken a look at that doc a few times but it seems to contradict what I'm learning at other times. That said things change and I trust that doc more. I've changed my code according to the doc and gotten it down to one error but now says I need to use it in an async function. When I do that I get even more errors, which doesn't make sense to me if what I'm using is supposed to be used in an async.
You need to see how old the docs you are reading are. Snapshot changed to event a few months ago. Always read the latest docs. You need to edit your question to show the latest code and errors.
Thank you. I've noticed the archived docs. How often do they change things? Anyway, I've edited my post and included the new code using the '!' to force the command. When I do that I have one error left stating what I said above. I've tried using if statements to create a null check but that still doesn't work.
Dart, Flutter and especially their packages are constantly evolving so it is usually a good idea to keep them up to date and to read what the changes are. The '!' doesn't force a command, it tells dart that you KNOW that the variable is not null. I suggest you also go through this dart.dev/guides/language/language-tour

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.