首页 > 解决方案 > 包含空安全库,现在代码未运行

问题描述

我使用了空安全库。现在我遇到了这个问题。请看截图。即使添加了空检查,它也不会。

在此处输入图像描述

body: FutureBuilder(
    future: allPerson(),
    builder: (context, snapshot) {
      if (snapshot.hasError) print(snapshot.error);
      return snapshot.hasData
          ? ListView.builder(
              itemCount: snapshot.data.length,
              itemBuilder: (context, index) {
                List list = snapshot.data;
                return Card(
                  child: ListTile(
                    title: Container(
                      width: 100,
                      height: 100,
                      child: Image.network(
                          "http://192.168.1.101/image_upload_php_mysql/uploads/${list[index]['image']}"),
                    ),
                    subtitle: Center(child: Text(list[index]['name'])),
                  ),
                );
              })
          : Center(
              child: CircularProgressIndicator(),
            );
    },
  ),

谢谢。

标签: flutterdart

解决方案


您可以为数据future函数的返回值指定类型。snapshot

FutureBuilder<List<dynamic>>(
        future: allPerson(),
        builder: (context, AsyncSnapshot<List<dynamic>> snapshot) {
          if (snapshot.hasError) print(snapshot.error);
          return snapshot.hasData
              ? ListView.builder(
                  itemCount: snapshot.data!.length,
                  itemBuilder: (context, index) { ...

推荐阅读