gpt4 book ai didi

flutter - Provider vs ValueNotifier Flutter

转载 作者:行者123 更新时间:2023-12-04 12:23:49 32 4
gpt4 key购买 nike

有人可以解释使用 Provider 之间的区别吗?包装和使用 ValueNofifier ?
现在我正在使用 ValueNotifierValueListenableBuilder在我的应用程序中,我看到这与使用 Providers 和 Consumers 之间有很多相似之处。两者都有使用提供的最新数据重建小部件的监听器,并且都使用 ChangeNotifier 和 notifyListeners。
那么有什么区别,我们什么时候应该选择其中一个而不是另一个?
谢谢

最佳答案

就我在应用程序中使用这两种东西的经验而言,主要区别在于

Provider can provide changes in any part of the app, like any where with the use of notifyListener(), and can be accessed using anywhere in the app. However, there is a possibility of bug in using a global ValueNotifier, which is not recommended. Technically, doesn't gives you much control on bug tracking when the code becomes big.

Provider(
create: (_) => MyModel(),
child: ...
)
其他主要区别:

Provider gives you the power to make use of Multiple Providers and can be stored in a single Provider array only, however, in ValueNotifier, you are ver much restricted to use one value at a time. For using multiple ValueNotifiers, you have to create multiple ValueNotifiers, and then dispose it all every time.

MultiProvider(
providers: [
Provider<Something>(create: (_) => Something()),
Provider<SomethingElse>(create: (_) => SomethingElse()),
Provider<AnotherThing>(create: (_) => AnotherThing()),
],
child: someWidget,
)
它基本上是将您的业务逻辑与您的正常应用程序逻辑分开的巧妙方式。

关于flutter - Provider vs ValueNotifier Flutter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63859266/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com