简体   繁体   English

通过 Future Provider Value 的 Flutter 列表视图

[英]Flutter List View through Future Provider Value

I am trying to call the output of a Future Provider of type Future> into a List View builder.我正在尝试将 Future> 类型的 Future Provider 的输出调用到列表视图构建器中。 I think I am very near as I am able to render the final List View itself, however, prior that, an error appears and is quickly replaced by the List View after completing the Future.我想我已经很接近了,因为我能够呈现最终的列表视图本身,但是,在此之前,会出现一个错误,并在完成 Future 后迅速被列表视图替换。 I believe there may be something wrong with my implementation there.我相信我在那里的实施可能有问题。

Here's what I've got so far (these are derivatives of my actual code, there's too many going on there that aren't necessary, I tried to simplify it):这是我到目前为止所得到的(这些是我实际代码的衍生物,那里有太多不必要的事情,我试图简化它):

class TempProvider extends ChangeNotifier(){
    List<Widget> _list = <Widget>[];
    List<Widget get list => _list;

    Future<List<Widget>> getList() async{
        List _result = await db....
        _result.forEach((_item){
            addToList(_item);
        });
    }

    addToList(Widget widget){
        _list.add(widget);
        notifyListeners();
    }
}

class Parent extends StatelessWidget{
    @override
    Widget build(BuildContext context) {
        return FutureProvider(
        create: (context) => TempProvider().getList(),
        child: Child(),
        );
    }   
}

class Child extends StatelessWidget{
    @override
    Widget build(BuildContext context) {
    var futureProvider = Provider.of<List<Widget>>(context);

        return FutureBuilder(
            initialData: <Widget>[],
            future: TempProvider().getList(),
            builder: (BuildContext context, AsyncSnapshot snapshot) {
                if (snapshot.connectionState == ConnectionState.none &&
                    snapshot.hasData == true) {
                    return ListView.builder(
                        itemCount: futureProvider.length,
                        itemBuilder: (BuildContext context, int index) {
                            return futureProvider[index];
                        },
                    );
                } else {
                    return Text('ALAWS');
                }
          },
        );
    }   
}

So basically, the output of my Future will be a list of widgets that will populate a List View that I am trying to build.所以基本上,我的 Future 的输出将是一个小部件列表,这些小部件将填充我正在尝试构建的列表视图。 Though I am able to render the list view in the end, the error below appears in between:虽然我最终能够呈现列表视图,但中间出现以下错误:

The getter 'length' was called on null.
Receiver: null
Tried calling: length
The relevant error-causing widget was
FutureBuilder<List<Widget>> 

Hoping someone can help with this one or at least give a better example.希望有人可以帮助解决这个问题,或者至少举一个更好的例子。

Thank you so much!非常感谢!

Not sure but, this is happening because your widget might be building twice and at first futureProvider is null and in second time it has some value.不确定,但是,发生这种情况是因为您的小部件可能会构建两次,并且一开始 futureProvider 为空,第二次它有一些价值。

Workaround:解决方法:

Replace this:替换这个:

futureProvider.length

With this:有了这个:

futureProvider?.length ?? 0

What the above code does?上面的代码是做什么的?

  1. futureProvider?.length : if futureProvider is null don't access it's length. futureProvider?.length :如果 futureProvider 为空,则不访问它的长度。
  2. Now the value returned will be null.现在返回的值将为空。
  3. ?? 0 ?? 0 : if the value returned is null then return 0 ; ?? 0 : 如果返回的值为空则返回0

You need to think over following things and edit your code.您需要考虑以下事项并编辑您的代码。

  1. At first place futureProvider should not be null.首先, futureProvider不应为空。
  2. Why are you not using snaphot.data when you are using FutureBuilder.为什么在使用 FutureBuilder 时不使用snaphot.data

So I have been doing my research and found the article below which shows a definite implementation based on what I need:所以我一直在做我的研究,发现下面的文章根据我的需要显示了一个明确的实现:

Flutter Provider Examples - Codetober Credits to Douglas Tober for the article. Flutter Provider 示例 - Codetober感谢 Douglas Tober 的文章。 Thanks again to Kalpesh for the quick help!再次感谢 Kalpesh 的快速帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM