2

I want to develop the banking app in flutter. But I get this error: Null check operator used on a null value I tried "flutter channel stable" but its not work and also tried '!' operator (as you can see in the code) but its not work... :(

Plese help me.. Note: Container widget is very long that's why I didn't choose to copy..

  Widget projectWidget() {
        return FutureBuilder<List<Histories>>(
            future: dbHelper.getHistories(),
            builder: (context, snapshot) {
              if (snapshot.hasData && snapshot.data!.length > 0) return Container();
              return ListView.builder(
                itemCount: snapshot.data!.length,
                itemBuilder: (context, index) {
                  Histories histories = snapshot.data![index];
                  return Container();
                },
              );
            });
      }

2 Answers 2

2

Inside the builder else condition is wrong. With the current code even if the snapshot has not data it will try to call snapshot.data!.length that's what causing null error.

Widget projectWidget() {
        return FutureBuilder<List<Histories>>(
            future: dbHelper.getHistories(),
            builder: (context, snapshot) {
              if (snapshot.hasData && snapshot.data!.length > 0) 
              return ListView.builder(
                itemCount: snapshot.data!.length,
                itemBuilder: (context, index) {
                  Histories histories = snapshot.data![index];
                  return Container();
                },
              );

               else 
                return Center(child: CircularProgressIndicator());
            });
      }
Sign up to request clarification or add additional context in comments.

Comments

1

You have a problem with your if statement, and in that way you try to access the data when it still don't exist, try something like this

return FutureBuilder<List<Histories>>(
        future: dbHelper.getHistories(),
        builder: (BuildContext context, AsyncSnapshot<List<Histories>> snapshot) {
          if (snapshot.connectionState == ConnectionState.done)  {
            return ListView.builder(
                itemCount: snapshot.data!.length,
                itemBuilder: (context, index) {
                  Histories histories = snapshot.data![index];
                      return Container();
                },
              );
          } else if (snapshot.connectionState == ConnectionState.waiting) {
            return Center(child: CircularProgressIndicator());
          } else {
            print('error');
            return Text('error');
          }
        });

or you can substitute this if (snapshot.connectionState == ConnectionState.done) with your if statement like this

return FutureBuilder<List<Histories>>(
        future: dbHelper.getHistories(),
        builder: (BuildContext context, AsyncSnapshot<List<Histories>> snapshot) {
          if (snapshot.hasData && snapshot.data!.length > 0)  {
            return ListView.builder(
                itemCount: snapshot.data!.length,
                itemBuilder: (context, index) {
                  Histories histories = snapshot.data![index];
                      return Container();
                },
              );
          } else if (snapshot.connectionState == ConnectionState.waiting) {
            return Center(child: CircularProgressIndicator());
          } else {
            print('error');
            return Text('error');
          }
        });

Comments

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.