He said state management have to say Google personally developed two state management
Widget
: the first one is
provide
the second one is
provider
.
This difference is a two out early, it did not seem a whole it updated. The second is the 2019 version came out only now is
provider: ^3.2.0
. On a stay in
provide: ^1.0.2
, basically the GG. But sometimes you might need for the project
provide
. So now I want to say is that these two basic usage, or is the contrast between the two use of it (state control multiple interfaces, the project may be more states to manage multiple interfaces.
Provide with the project previously encountered a problem later changed Provider, build reconstruction. provide will not rebuild, provider will be rebuilt.
To solve the problem: you can choose not to listen
the listen
: false or change to initstate.
First, create
1, create aprovide,还用上次例子的counter
Import ' Package: Flutter / material.dart ' ; class Counter with ChangeNotifier { int value = 0 ; INCREMENT () { value ++ ; notifyListeners (); // change notification listener } }
2, create aprovider
import 'package:flutter/material.dart'; class Counter with ChangeNotifier { int _count = 0; get count => _count; void increment() { _count++; notifyListeners(); //通知 } }
Second, top-dependent
1、provide
void main() { //顶层依赖 var counter = Counter(); var providers = Provider(); providers ..(Provider<Counter>.value(counter)); runApp(ProviderNode(child: MyApp(), providers: providers)); }
2、provider
void main() { runApp( MultiProvider( providers: [ ChangeNotifierProvider.value(value: Counter()), //ChangeNotifierProvider(builder: (_) => Counter()), ], child: MyApp(), ), ); }
Third, the use
1、provide
Provide.value <Counter> (context) .increment (); // with the increment off method. . Here you can also pass parameters into account simply write the Counter inside the increment which accepts two parameters like
2、provider
Provider.of <Counter> (context, the listen: to false ) .increment (); // here may also pass parameters
Fourth, get the value
1、provide
return Provide<ZxxxDetailsProvide>( builder: (context,child,val){ var goodsInfo = Provide.value<ZxxxDetailsProvide>(context).goodsInfo.data; } );
2、provider
return Consumer<ZxxxListProvide>( builder: (context,model,child){ ... } );
Important: use provider
the time we have to pay attention to a certain set listen to the Bool value is such
await Provider.of<DetailsInfoProvide>(context, listen: false).getGoodsInfo(goodsId);