原文鏈接:http://www.fackyou.org/archives/flutter/2020062943.html
講道理這個用法是我從fish-redux的issus的別人的反饋的官方(貌似)提供的解決方案里扒出來的州既,以我的能力暑竟,目前在百度上搜不到!
LocalProps解決了什么問題瘾敢?
比方說:
你在component里面有個插件镐侯,然后這個插件需要一個controller潮秘,這個controller為了在effect或者reducer里面能夠用到摧找,所以你會存到state中去,按照fish-redux的尿性這個state必須在page中定義然后通過conn來傳遞到component中伺糠。如果你只是在component中定義并初始化,在頁面啟動的時候會告訴你view里的controller沒有被初始化斥季,所以你不得不在page中初始化好了再用训桶。然后你在component的effect或者reducer中調用的時候會發(fā)現(xiàn)丫根本沒有任何用。因為你需要dispatch到page的action中才能有用酣倾,因為component中的controller的state并不會被改變(可能是這樣舵揭,如果說錯了,你當我沒說……反正就是用不了)
這樣很尷尬霸钚A鹦唷!
如果你的page下有好幾個component稚铣,然后每個component都有各自的controller箱叁,你還得給他區(qū)分,還要把所有controller的操作寫在page里面惕医,那還分毛個component案!
LocalProps的優(yōu)點
component自己保管自己的數(shù)據抬伺,不需要從page通過conn傳遞螟够,自己可以修改自己的數(shù)據,不需要出發(fā)page的action峡钓!
LocalProps的缺點
LocalProps的state即使修改了也不會觸發(fā)刷新view妓笙,所以如果涉及到view中的數(shù)據,就不要存LocalProps了能岩,會讓你抓狂寞宫!
LocalProps的用法
重點來了!
在state.dart里增加一段:
class ComponentLocalProps extends LocalProps<ComponentLocalProps> {
final RefreshController refreshController =
RefreshController(initialRefresh: false);
ComponentLocalProps(Context<Object> ctx) : super(ctx);
factory ComponentLocalProps.of(ExtraData ctx) {
return LocalProps.provide((_) => ComponentLocalProps(_)).of(ctx);
}
@override
void destructor(Context<Object> ctx) {
refreshController.dispose();
}
}
里面的refreshController是我定義的state數(shù)據拉鹃,也就是說把從page傳遞過來的state數(shù)據和LocalProps的state數(shù)據區(qū)分開來了辈赋,其他的不需要改,就destructor
方法里你得把state銷毀膏燕,這個好像還是很死板的樣子钥屈!
然后使用的時候在view.dart里就是這樣:
ComponentLocalProps.of(viewService).refreshController
在effect.dart里是這樣:
ComponentLocalProps.of(ctx).refreshController.loadComplete();
在reducer.dart里是:
想什么呢?reducer里面有context嗎坝辫?
看完之后如果你覺得有用篷就,請在原文鏈接的留言里告訴我,如果沒用……你自己研究去阀溶!