0
votes

I'm trying to read the ID of each doc in a collection. As I can saw, doc.id is a string, and it has the value name for each doc. So I just tried to add that value to a list, for later pass it to a DropDownButton. But for some reason, the list return null.

  List<String> readthishit() {
    List<String> ex;
    FirebaseFirestore.instance
        .collection('Enrollment')
        .get()
        .then((QuerySnapshot querySnapshot) => {
              querySnapshot.docs.forEach((doc) {
                ex.add(doc.id);
              })
            });
    return ex;
  }

What's happening?

1

1 Answers

1
votes

You need to use the await keyword & wait for the result from Firebase.

Currently, you are sending a call to the Firebase but, before the result from Firebase, your code is returning the null list ex.

    Future<List<String>> readThisShit() async {
      List<String> ex = <String>[];
      final querySnapshot = await FirebaseFirestore.instance
          .collection('Enrollment')
          .get();


      querySnapshot.docs.forEach((doc) {
          ex.add(doc.id);
      });

      return ex;
  }

Also, I think you should use lowerCamelCase notation for your method names. So, readthisshit will become readThisShit.