參考文檔:
http://www.ruanyifeng.com/blog/2016/09/redux_tutorial_part_one_basic_usages.html
http://www.ruanyifeng.com/blog/2016/09/redux_tutorial_part_two_async_operations.html
https://segmentfault.com/a/1190000015579633
flutter-redux脸秽,主要用于項(xiàng)目的狀態(tài)管理
通過store保存保存所有的數(shù)據(jù)
state是某個(gè)時(shí)點(diǎn)的數(shù)據(jù)猩谊,通過store.subscript監(jiān)聽state的變化
state的數(shù)據(jù)與被渲染到view層
view 層通過store.dispatch()發(fā)出通知action給store修改狀態(tài)state
發(fā)出action揪罕,重新計(jì)算state的過程被成為reducer讹堤,這個(gè)過程中可以設(shè)置中間件
class MyApp extends StatelessWidget {
final Store<int> store;
MyApp({this.store});
@override
Widget build(BuildContext context) {
return new StoreProvider(store: store, child: new MaterialApp(
home: new StoreConnector(builder: (BuildContext context,int counter){
return new MyHomePage(title: 'Flutter Demo Home Page',counter:counter);
}, converter: (Store<int> store){
return store.state;
}) ,
));
}
}
使用flutter-redux吆鹤,最外層要被StoreProvider控件包裹,否則會報(bào)錯
new StoreConnector(builder: (BuildContext context,int counter){
return new MyHomePage(title: 'Flutter Demo Home Page',counter:counter);
}, converter: (Store<int> store){
return store.state;
})
使用StoreConnector控件將state傳遞到view洲守,一定需要使用converter將Store中的狀態(tài)轉(zhuǎn)變成組件需要的狀態(tài),這里的builder函數(shù)的第二個(gè)參數(shù)就是converter函數(shù)的返回值