【问题标题】:Flutter Error: Null check operator used on a null valueFlutter 错误:对空值使用空检查运算符
【发布时间】:2021-11-10 07:10:57
【问题描述】:

我想在 Flutter 中开发银行应用程序。 但我收到此错误: Null check operator used on a null value 我尝试了“flutter channel stable”,但它不起作用,还尝试了“!”运算符(如您在代码中所见),但它不起作用... :(

请帮帮我.. 注意:容器小部件很长,所以我没有选择复制..

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

【问题讨论】:

    标签: android ios flutter dart null


    【解决方案1】:

    你的 if 语句有问题,通过这种方式你尝试访问仍然不存在的数据,试试这样的方法

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

    或者你可以用你的 if 语句替换这个if (snapshot.connectionState == ConnectionState.done)

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

    【讨论】:

      【解决方案2】:

      在构建器内部 else 条件是错误的。使用当前代码,即使快照没有数据,它也会尝试调用snapshot.data!.length,这就是导致空错误的原因。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-04
        • 2022-07-04
        • 2021-09-25
        • 2021-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-18
        相关资源
        最近更新 更多