Home > database >  Flutter Error: Null check operator used on a null value
Flutter Error: Null check operator used on a null value

Time:09-16

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();
                },
              );
            });
      }

CodePudding user response:

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());
            });
      }

CodePudding user response:

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');
          }
        });
  • Related