(扑)使用FutureBuilder的ListView.builder不起作用 [英] (Flutter) ListView.builder using FutureBuilder not working

查看:283
本文介绍了(扑)使用FutureBuilder的ListView.builder不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在中使用 ListView.builder ListView 中显示项目FutureBuilder 。我的 FutureBuilder future 函数如下所示:

I am trying to display items in a ListView using ListView.builder inside a FutureBuilder. My future function for FutureBuilder looks like this:

_fetchListItems() async {
   wait() async {
     number = await db.getNumber(userId); }

   await wait();
 List rawFavouriteList = await db.getList(number);

  setState((){
  rawFavouriteList.forEach((item){
       _faouriteList.add(Model.map(item));
           }});
return _faouriteList;
}

我的 FutureBuilder 看起来像这样:

            FutureBuilder(
               future: _fetchListItems(),
               builder:(context, AsyncSnapshot snapshot) {
                     if (!snapshot.hasData) {
                   return Center(child: CircularProgressIndicator());
         } else {Container( child: ListView.builder(                                                  
                itemCount: _faouriteList.length,
                scrollDirection: Axis.horizontal,
             itemBuilder: (BuildContext context, int index) {
                  return Text(                                                 
                   '${_faouriteList[index].title}');
              }));}})




在声明之后引发了构建FutureBuilder(脏,状态:
I / flutter(24728):_FutureBuilderState#f12a3):
I / flutter(24728):构建函数返回null。
I / flutter(24728):令人讨厌的小部件是:FutureBuilder
I / flutter(24728):构建函数绝不能返回null

he following assertion was thrown building FutureBuilder(dirty, state: I/flutter (24728): _FutureBuilderState#f12a3): I/flutter (24728): A build function returned null. I/flutter (24728): The offending widget is: FutureBuilder I/flutter (24728): Build functions must never return null

另一个抛出异常:构建函数返回null。

Another exception was thrown: A build function returned null.

注意:

我尝试从 initState 调用 _fetchListItems()而不使用 FutureBuilder ,这对我也不起作用。

I tried to call _fetchListItems() from initState and not use FutureBuilder and that didn't work for me as well.

这里是该情况的链接:(扑打/飞镖)initState中的两个异步方法不起作用

Here is a link to that case: (Flutter/Dart) Two async methods in initState not working

请让我知道如果我应该使用 FutureBuilder initState 等待 List 加载它的数据。以及如何使其工作,因为似乎没有一种方法对我有用:(

Please let me know if I should use FutureBuilder or initState to wait for List to load it's data. And how to make it work since none of the methods seem to work for me :(

推荐答案

您获取的 _fetchListItems 方法不是问题。

Your fetch _fetchListItems method is not the problem as far as I can tell.

您的错误消息非常清楚,构建函数返回null在这种情况下,返回null的方法是作为生成器参数传递给 FutureBuilder 的匿名函数。您只是错过了 return 关键字,因为您只是创建容器的实例,而不返回它。

Your error message is very clear, " a build function returned null". In this case, the method that returns null is the anonymous function passed as the builder argument to the FutureBuilder. You're just missing the return keyword inside the else case because you're just creating the instance of Container but not returning it.

类似这样的东西:

FutureBuilder(
    future: _fetchListItems(),
    builder:(context, AsyncSnapshot snapshot) {
        if (!snapshot.hasData) {
            return Center(child: CircularProgressIndicator());
         } else {
            return Container(
                child: ListView.builder(                                                  
                    itemCount: _faouriteList.length,
                    scrollDirection: Axis.horizontal,
                    itemBuilder: (BuildContext context, int index) {
                        return Text('${_faouriteList[index].title}');                                           
                    }
                )
            );
         }
     }
)

我不知道您的代码还有其他问题,但这应该可以解决您所询问的特定错误消息。

I don't know if there are any other problems with your code but this should solve the particular error message you are asking about.

这篇关于(扑)使用FutureBuilder的ListView.builder不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆