Flutter Error: Null check operator used on a null value

Flutter Error: Null check operator used on a null value

我想在 flutter 中开发银行应用程序。 但是我得到这个错误:空检查运算符用于空值 我试过“颤动通道稳定”但它不起作用并且也试过'!'运算符(如您在代码中所见)但它不起作用...:(

请帮帮我.. 注意:Container widget很长所以我没有选择复制..

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

你的 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 (snapshot.connectionState == ConnectionState.done) 替换为您的 if 语句,例如

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

builder里面的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());
            });
      }