何时使用Provider.of< X>与消费者X在Flutter [英] When to use Provider.of<X> vs. Consumer<X> in Flutter

查看:129
本文介绍了何时使用Provider.of< X>与消费者X在Flutter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然在扑朔迷离地关注状态管理技术,对于何时何地使用 Provider.of< X> Consumer< X> 。我从文档了解(我认为),在这两者之间进行选择时,当我们要访问数据时可以使用Provider.of,但不需要更改UI。因此,以下内容(来自文档)可以访问数据并在发生新事件时更新UI:

  return HumongousWidget(
// ...
子对象:AnotherMonstrousWidget(//<-此小部件将根据新的数据事件重建
// ...
子对象:Consumer< CartModel>(
生成器:(上下文,购物车,子项){
return Text('Total price:$ {cart.totalPrice}');
},
),
) ,
);

在这里,我们只需要数据而不希望使用UI进行重建,将 Provider.of< X> listen 参数设置为 false ,如下所示:

  Provider.of< CartModel>(上下文,监听:false).add(item); \\小部件不会重建

但是,收听不是必需的,因此以下内容也将运行:

  Provider.of< CartModel>(上下文)。新增项目); ener侦听器可选

所以这给我带来了几个问题:


  1. 这是区分 Provider.of< X> Consumer< ; X> 。前者不会更新UI,后者不会吗?

  2. 如果 listen 未被设置为 false 默认情况下会重新构建小部件还是不重新构建?如果 listen 设置为 true 怎么办?

  3. 为什么要<< c $ c> Provider.of ,当我们有 Consumer

  4. 时,可以选择完全重建UI。 ol>

    解决方案

    没关系。但是要快速解释一下:



    Provider.of 唯一获取方式并听一个对象。
    Consumer Selector 和所有* ProxyProvider调用 Provider.of 上班。



    Provider.of Consumer 是个人喜好问题。但这两者都有一些论点。



    Provider.of




    • 在所有窗口小部件生命周期中,包括点击处理程序和 didChangeDependencies

    • 不会增加缩进



    消费者




    • 允许重新构建更精细的小部件

    • 解决了大多数BuildContext滥用问题


    I'm still wrapping my head around state-management techniques in flutter and am a bit confused about when and why to use Provider.of<X> vs. Consumer<X>. I understand (I think) from the documentation that when choosing between these two you would use Provider.of when we want access to the data, but you don't need the UI to change. So the following (taken from the docs) gets access to the data and updates the UI on new events:

    return HumongousWidget(
      // ...
      child: AnotherMonstrousWidget(// <- This widget will rebuild on new data events
        // ...
        child: Consumer<CartModel>(
          builder: (context, cart, child) {
            return Text('Total price: ${cart.totalPrice}');
          },
        ),
      ),
    );
    

    Whereas, where we only need the data on don't want to rebuild with UI, we'd use Provider.of<X> with the listen parameter set to false, as below:

    Provider.of<CartModel>(context, listen: false).add(item); \\Widget won't rebuild
    

    However, listen isn't required and so the following will run too:

    Provider.of<CartModel>(context).add(item); \\listener optional
    

    So this brings me to a few questions:

    1. Is this the correct way to distinguish Provider.of<X> and Consumer<X>. Former doesn't update UI, latter does?
    2. If listen isn't set to false will the widget be rebuilt by default or not rebuilt? What if listen is set to true?
    3. Why have Provider.of with the option to rebuild the UI at all when we have Consumer?

    解决方案

    It doesn't matter. But to explain things rapidly:

    Provider.of is the only way to obtain and listen to an object. Consumer, Selector, and all the *ProxyProvider calls Provider.of to work.

    Provider.of vs Consumer is a matter of personal preference. But there's a few arguments for both

    Provider.of

    • can be called in all the widgets lifecycle, including click handlers and didChangeDependencies
    • doesn't increase the indentation

    Consumer

    • allows more granular widgets rebuilds
    • solves most BuildContext misuse

    这篇关于何时使用Provider.of&lt; X&gt;与消费者X在Flutter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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