Home > Net >  The getter 'length' was called on null. The relevant error-causing widget was StreamBuilde
The getter 'length' was called on null. The relevant error-causing widget was StreamBuilde

Time:11-14

I have been learning Flutter for some time trying to understand from existing Flutter projects, so the code that will follow is not mine.

Currently I am testing a project but I am facing an error that I had never seen.

The getter 'length' was called on null.
Receiver: null
Tried calling: length

I think it comes from this part of the code.

StreamBuilder<List<DocumentSnapshot>>(
                    stream: postListBloc.postStream,
                    builder: (context, snapshot) {
                      if (snapshot.connectionState == ConnectionState.waiting)
                        return Center(
                          child: CircularProgressIndicator(),
                        );
                      else {
                        int presentLength = snapshot.data.length;
                        return ListView.builder(
                            physics: NeverScrollableScrollPhysics(),
                            shrinkWrap: true,
                            itemCount: snapshot.data.length,
                            itemBuilder: (context, index) {
                              DocumentSnapshot documentSnapshot =
                                  snapshot.data[index];
                              String id = documentSnapshot.id;
                              debugPrint('${snapshot.data.length}');
                              return Column(children: [
                                Padding(
                                    padding: EdgeInsets.only(bottom: 10),
                                    child: PostCardView(
                                        documentSnapshot.get('community'),
                                        id,
                                        true)),
                                (index != snapshot.data.length - 1)
                                    ? Container()
                                    : buildProgressIndicator(presentLength)
                              ]);
                            });
                      }
                    },
                  ),

I have searched here for different solutions but so far nothing has worked.

If anyone knows how to solve this problem.

CodePudding user response:

Basically the error says there's no data in your snapshot.data its null, check if its not null first if it null show no data available if its not null show the data

CodePudding user response:

You can do this in two way:

1- set initial data in stream builder to an empty list like this :

initialData : []

2- the other way is to check whether snapshot has data or not with:

snapshot.hasData

CodePudding user response:

This is called null safety in dart language since you said you're trying to understand from existing Flutter projects, the problem is this project is built in flutter version wich doesn't support null safety . Downgrade flutter version to the version of the project. I recommand you to learn about null safety because its very important.

  • Related