【问题标题】:FutureBuilder only works in DebugFutureBuilder 仅适用于 Debug
【发布时间】:2019-12-12 01:00:53
【问题描述】:

我有一个带有 ListView 的 FutureBuilder 来显示具有从 .txt 文件中读取的值的自定义项(小部件)。

问题是这些项目只有在我以调试模式或运行模式启动应用程序时才会显示。当我尝试使用 AppLauncher 打开应用程序时(就像“普通”用户那样),listView 是空的。我在 AVD 和“真实”设备上进行了尝试。

Future "listFuture" 用于从文件中读取值并返回一个 Widget 列表

class Home extends StatefulWidget {
  final Future listFuture = setupList();

  @protected
  @mustCallSuper
  void initState() {
    print("init complete");
  }

  @override
  State<StatefulWidget> createState() {
    return HomeState();
  }
}

如果 FutureBuilder 正确获取数据,则应显示带有我的小部件列表的 listView

child: FutureBuilder<List<SubListItem>>(
                      future: widget.listFuture,
                      // ignore: missing_return
                      builder: (BuildContext context, AsyncSnapshot snapshot) {
                        switch (snapshot.connectionState) {
                          case ConnectionState.none:
                            return new Text("None");
                          case ConnectionState.waiting:
                            return new Text("loading");
                          default:
                            if (snapshot.hasError) {
                              print("Error");
                              return Center(child: (Text("No data")));
                            } else {
                              return subListView(context, snapshot);
                            }
                        }
                      },
                    ),
Widget subListView(BuildContext context, AsyncSnapshot snapshot) {
  List<Widget> items = snapshot.data;

  //This ScrollConfiguration is used to remove any animations while scrolling
  return ScrollConfiguration(
    behavior: CustomScrollBehavior(),
    child: Container(
      padding: EdgeInsets.symmetric(horizontal: 4),
      child: new ListView.builder(
        itemCount: items.length,
        itemBuilder: (BuildContext context, int index) {
          return Column(
            children: <Widget>[items[index]],
          );
        },
      ),
    ),
  );
}

感谢您的帮助!

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    这看起来像是异步数据问题,请尝试以下更改:

    • 从您的StatefulWidget 中删除listFuture
    • 在您的State 中添加listFuture 变量。
    • setupList() 方法移到State 中。
    • 最后直接这样调用:
    child: FutureBuilder<List<SubListItem>>(
                          future: setupList(),
                          // ignore: missing_return
                          builder: (BuildContext context, AsyncSnapshot snapshot) {
                               if(!snapshot.hasData) {
    
                                return new Text("loading");
                               }
                               else  if (snapshot.hasError) {
                                  print("Error");
                                  return Center(child: (Text("No data")));
                                } else {
                                  return subListView(context, snapshot);
                                }
                            }
                          },
                        ),
    

    【讨论】:

    • 我已经尝试过了,但是如果您在 applauncher 上打开应用程序并导航到新页面 (Navigator.push) 然后按返回按钮 ListView 正确显示,我发现了一个新错误。
    【解决方案2】:

    好的,我解决了这个问题。您只需在构建 Widget 时调用“setState”即可。

      @protected
      @mustCallSuper
      void initState() {
        super.initState();
        Future.delayed(Duration.zero, () {
          //This setState is necessary because it refreshes the listView
          setState(() {});
        });
      }
    

    【讨论】:

    • 不,你不需要那种技巧,看看我更新的答案
    • 很抱歉,但这在我的情况下仍然不起作用。也许这是其他人的解决方案,但不适用于我。不过还是谢谢你。
    猜你喜欢
    • 2022-01-05
    • 2011-11-16
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    • 2013-09-04
    • 2020-12-31
    • 1970-01-01
    相关资源
    最近更新 更多