0

I am receiving the above null check error code with snapshot. I have investigated a number of similar questions on Stack overflow, but don't seem to get a fix to my problem. I am fairly new to flutter still and would really appreciate some help.

Here is my code.

  var db = DatabaseConnect();

  Todolist({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: FutureBuilder(
          future: db.getTodo(),
          initialData: const [],
          builder: (BuildContext context, AsyncSnapshot<List> snapshot) {
            var data = snapshot.data;
            var datalength = data!.length;

            return datalength == 0
                ? const Center(
                    child: Text('no data found'),
                  )
                : ListView.builder(
                    itemCount: datalength,
                    itemBuilder: (context, i) => Todocard(
                      id: data[i].id,
                      title: data[i].title,
                      creationDate: data[i].creationDate,
                      isChecked: data[i].isChecked,
                      insertFunction: () {},
                      deleteFunction: () {},
                    ),
                  );
          }),
    );
  }
}
Tobsig
  • 33
  • 1
  • 9

2 Answers2

0

It's because the data you are getting from snapshot.data is null. Instead of storing it in the variable data try using print(snapshot.data); and then you'll see that it returns null. So when you check if datalength == 0 instead of trying to check snapshot.data == null it will solve your problem.

digito_evo
  • 3,216
  • 2
  • 14
  • 42
Yash Bhansali
  • 420
  • 2
  • 6
0

snapshot.data will first return null when the data is being fetched and that is why the result of snapshot.data is nullable.

You're getting the error because you're using the null-check operator on the data variable in its null state in this line below:

var datalength = data!.length;

Solution:

You should check your data variable if it is null and show a progress indicator if that is true.

If the null check is false, you can get the length of the data.

Then you display the empty state message if the length is empty and display the actual data if the length is not empty.

Your builder method can be updated to this below:

builder: (BuildContext context, AsyncSnapshot<List> snapshot) {
  var data = snapshot.data;
  if (data == null) {
    return const Center(child: CircularProgressIndicator());
  } else {
    var datalength = data.length;
    if (datalength == 0) {
      return const Center(
        child: Text('no data found'),
      );
    } else {
      return ListView.builder(
        itemCount: datalength,
        itemBuilder: (context, i) => Todocard(
          id: data[i].id,
          title: data[i].title,
          creationDate: data[i].creationDate,
          isChecked: data[i].isChecked,
          insertFunction: () {},
          deleteFunction: () {},
        ),
      );
    }
  }
}),
Victor Eronmosele
  • 7,040
  • 2
  • 10
  • 33