Flutter:FutureBuilder NoSuchMethodError isNotEmpty

Flutter: FutureBuilder NoSuchMethodError isNotEmpty

我用 ListView.builder 使用 FlutterBuilder 创建了一个 flutter 项目,它从 JSON 文件(本地)获取数据。我成功构建了该应用程序,但只有一秒钟的屏幕显示如下:

在那之后,我的应用程序运行顺利。 这是我的代码:

FutureBuilder(
                future: _isInit ? fetchDoa(context) : Future(null),
                builder: (context, _) {
                  if (doaList.isNotEmpty) {
                    return ListView.builder(
                      itemCount: doaList.length,
                      itemBuilder: (BuildContext context, int index) {
                        Doa doa = doaList[index];
                        return Card(
                            margin: EdgeInsets.all(8),
                            child: ListTile(
                                title: Text(doa.judul),
                                onTap: () {
                                  Navigator.of(context).push(MaterialPageRoute(
                                      builder: (BuildContext context) =>
                                          DoaPage(
                                            doa: doa,
                                          )));
                                },
                                trailing: IconButton(
                                  icon: Icon(
                                    doa.fav
                                        ? Icons.favorite
                                        : Icons.favorite_border,
                                    color: doa.fav ? Colors.red : null,
                                  ),
                                  onPressed: () => setState(() {
                                    doa.fav = !doa.fav;
                                  }),
                                )));
                      },
                    );
                  }
                  return CircularProgressIndicator();
                })

这是我的应用程序的完整预览:

我需要你的帮助,非常感谢:)

改变这个:

if (doaList.isNotEmpty)

进入这个:

if (doaList != null)

这是因为您在以下行中有一个空数据:

if (doaList.isNotEmpty)

你的 FutureBuilder 有点不正确。

您需要将 FutureBuilder 更新为如下内容:

  // Change YourDoaList to your model.
  FutureBuilder<YourDoaList>(
    future: _isInit ? fetchDoa(context) : Future(null),
    builder: (BuildContext context, AsyncSnapshot<YourDoaList> snapshot) {
      Widget widget;
      if (snapshot.hasData) {
        // You have succesfully get the data.
        // check if you have correct data here
        YourDoaList items = snapshot.data;

        widget = ListView.builder(
           ...
        );
      } else if (snapshot.hasError) {
        // You've failed get the data
        widget = Text("Failed");
      } else {
        // waiting for data
        widget = CircularProgressIndicator();
      }

      return widget;
    },
  ),

在此处查看文档:https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html

将生成器设置为

builder: (context, snapshot) {
 if(snapshot.hasData){
    if (doaList != null && doaList.isNotEmpty){
    // UI
    } else {
    // no data available
  }

 } else {
   // show loader or empty container
 }

虽然有很多好的答案。但是按照你现在的代码。只需在您定义 DAO 列表的位置添加 {}

喜欢

var daoList = [];

官方文档中的一件重要事情Link

The future must have been obtained earlier, e.g. during State.initState, State.didUpdateWidget, or State.didChangeDependencies. It must not be created during the State.build or StatelessWidget.build method call when constructing the FutureBuilder. If the future is created at the same time as the FutureBuilder, then every time the FutureBuilder's parent is rebuilt, the asynchronous task will be restarted.

所以在你的 initState 中,做

@override
  void initState() {
    myFuture = getFuture();
    super.initState();
  }

然后在 futureBuilder 中。

FutureBuilder(
    future: myFuture,
    remaining code
........