gpt4 book ai didi

flutter provider changenotifierprovider 问题

转载 作者:行者123 更新时间:2023-12-03 02:40:59 28 4
gpt4 key购买 nike

我正在查看 flutter 网站上的以下代码:

void main() {
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => CartModel()),
Provider(create: (context) => SomeOtherClass()),
],
child: MyApp(),
),
);
}


我想知道,Provider 和 ChangeNotifierProvider 有什么区别?

谢谢!

最佳答案

来自 provider package documentation (一直往下):

Provider: The most basic form of provider. It takes a value and exposes it, whatever the value is.

ListenableProvider: A specific provider for Listenable object. ListenableProvider will listen to the object and ask widgets which depend on it to rebuild whenever the listener is called.

ChangeNotifierProvider: A specification of ListenableProvider for ChangeNotifier. It will automatically call ChangeNotifier.dispose when needed.



所以, ChangeNotifierProviderProvider 的特定类型它将监听该对象并在此对象更新后重建其依赖的小部件。此外,它会自动调用 dispose需要时的方法。
Provider是通用提供程序,没有任何更复杂的功能,非常像优化的 Inherited Widget .

关于flutter provider changenotifierprovider 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59883666/

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