It'sPhil
It'sPhil

Reputation: 117

Exception has occurred. _TypeError (type 'Null' is not a subtype of type 'Widget') on ListView.builder

I am trying to get the snapshot of members from my fbbackend but suddenly I get this error. type 'Null' is not a subtype of type 'Widget' code:

ListView.builder(
                        itemCount:  null,
                        shrinkWrap: true,
                        itemBuilder: (context, index) {
                          if (snapshot1.hasData) {
                            //what ever you want
                     String val = snapshot1.data?.get('members')[index];
                                   //HERE(In front of [index])  👆
//RangeError (RangeError (index): Invalid value: Not in inclusive range 0..1: 2)
                            return Text(val);
                          } else {
                            return const LoadingScreen();
                          }
                        },
                        ),

Try to get snapshot of array in "members"

What should I do to fix this?

Upvotes: 0

Views: 774

Answers (1)

brook yonas
brook yonas

Reputation: 526

you need to check if the snapshot is null or not null before using them

    ListView.builder(
                        itemCount:  null,
                        shrinkWrap: true,
                        itemBuilder: (context, index) {
                          if (snapshot1.hasData) {
                            //what ever you want
                     String val = snapshot1.data?.get('members')[index];
                                     HERE(In front of [index])  👆
//RangeError (RangeError (index): Invalid value: Not in inclusive range 0..1: 2)
                            return Text(val);
                          } else {
                            return const LoadingScreen();
                          }
                        },
                        ),

you can get the specific documentsnapshot like this

StreamBuilder(
  stream: FirebaseFirestore.instance.collection("groups")
   .doc(groupId).snapshots(),
    builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
    if (!snapshot.hasData) {
      return Text("Loading");
    }
    var userDocument = snapshot.data["members"];
    return ListView.builder(
    itemCount: userDocument.length,
      shrinkWrap: true,
      itemBuilder: (context, index) {
        return Text(userDocument[index]);
             ),
      }
  );

Upvotes: 1

Related Questions