Flutter

Flutter官方介绍Provider:https://flutter.dev/docs/development/data-and-backend/state-mgmt/simple

模型继承ChangeNotifier,ChangeNotifierProvider(或者MultiProvider)包裹祖先wdget并提供模型实列,Consumer包裹使用模型实列的子widget。

Provider.of<模型>(context, listen: false)可不在使用Consumer的地方获取到模型实列,然后对模型实例进行操作。

ChangeNotifierProvider在使用的时候,可能会在被包裹的祖先wdget中使用实例模型,这时可将这部分代码实现在ChangeNotifierProvider的builder属性中,然后将builder属性中的context传入相应的代码。

Consumer有一个child属性,可以将不依赖模型实例的widget赋给child,然后builder属性中引用child即可。

原文地址:https://www.cnblogs.com/yarightok/p/13776759.html