简体   繁体   English

Null 检查运算符用于 null 值 (FutureBuilder)

[英]Null check operator used on a null value (FutureBuilder)

I am receiving the above null check error code with snapshot.我收到上述 null 检查错误代码和快照。 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.我对 flutter 还是很陌生,非常感谢一些帮助。

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

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);这是因为您从 snapshot.data 获得的数据是 null.. 而不是将其存储在变量数据中.. 尝试使用 print(snapshot.data); and then you'll see that it returns null.. so when you check if datalength == 0.. instead of thag try checking snapshot.data == null and it will solve your problem.. do upvote if helpful然后你会看到它返回 null .. 所以当你检查 datalength == 0 .. 而不是 thag 尝试检查 snapshot.data == null 它会解决你的问题.. 如果有帮助,请点赞

snapshot.data will first return null when the data is being fetched and that is why the result of snapshot.data is nullable.在获取数据时, snapshot.data将首先返回 null,这就是为什么snapshot.data的结果可以为空的原因。

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:您收到错误是因为您在下面这一行中的 null state 中的data变量上使用了空检查运算符:

var datalength = data!.length;

Solution:解决方案:

You should check your data variable if it is null and show a progress indicator if that is true.您应该检查您的data变量是否为 null,如果为真则显示进度指示器。

If the null check is false, you can get the length of the data.如果null检查为假,则可以得到数据的长度。

Then you display the empty state message if the length is empty and display the actual data if the length is not empty.然后,如果长度为空,则显示空的 state 消息,如果长度不为空,则显示实际数据。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM