Home > Software design >  Flutter Firebase - Null check operator used on a null value, can't get Field
Flutter Firebase - Null check operator used on a null value, can't get Field

Time:09-05

I got a field "user_collections" which is a map. The entries of those maps are Arrays. The goal is that a user can create a collection (name of the array-field) and put game id's in it. Somehow I can get the field "user_collections" but I am not able to get the array data which is stored in the collection.

Here is the Widget that I want to create:

Widget build(BuildContext context) {
return StreamBuilder(
    stream: DatabaseService.instance.getUserCollections(),
    builder: (BuildContext context,
        AsyncSnapshot<DocumentSnapshot<Map<String, dynamic>>> snapshot) {
      if (!snapshot.hasData) {
        return Center(
          child: Text("Lädt ..."),
        );
      }
      return Container(
          child: ListView.separated(
              separatorBuilder: (BuildContext context, int index) {
                return SizedBox(
                  width: 5,
                );
              },
              itemCount: 1,
              itemBuilder: (context, mainIndex) {
                return GestureDetector(
                  child: Container(
                    child: Text(
                        snapshot.data!.data()!.values.first.toString()),
                  ),
                  onTap: () {},
                );
              }));
    });

}

And here is the DatabaseService-Method to get the field:

Stream<DocumentSnapshot<Map<String, dynamic>>> getUserCollections() {
    return FirebaseFirestore.instance
        .collection("users")
        .doc("user_collections")
        .snapshots();
  }

It creates following Error: Null check operator used on a null value

And here is how the Firebase looks: Screenshot of Firebase

Do you have any ideas why I recieve the error? Thanks in advance!!

CodePudding user response:

Please try

snapshot.data!.get().data()! ...
  • Related