Provider 与 ValueNotifier 颤振
Provider vs ValueNotifier Flutter
谁能解释一下使用 Provider package and using ValueNofifier 的区别?
现在我正在我的应用程序中使用 ValueNotifier and ValueListenableBuilder,我发现这与使用 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,
)
这基本上是一种将业务逻辑与普通应用程序逻辑分开的巧妙方法。
谁能解释一下使用 Provider package and using ValueNofifier 的区别?
现在我正在我的应用程序中使用 ValueNotifier and ValueListenableBuilder,我发现这与使用 Providers 和 Consumers 之间有很多相似之处。两者都有使用提供的最新数据重建小部件的侦听器,并且都使用 ChangeNotifier 和 notifyListeners。
那么有什么区别,什么时候我们应该选择其中一个而不是另一个?
谢谢
就我在应用程序中使用这两种东西的经验而言,主要区别在于
Provider
can provide changes in any part of the app, like any where with the use ofnotifyListener()
, and can be accessed using anywhere in the app. However, there is a possibility of bug in using a globalValueNotifier
, 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, inValueNotifier
, 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,
)
这基本上是一种将业务逻辑与普通应用程序逻辑分开的巧妙方法。