我有一个包含在RefreshIndicator
中的列表。当列表被拉下来时,我想从Firestore(数据库)中获取一些数据,更新我的状态,然后渲染列表。
我有一个连接的redux动作可以获取数据并更新我的状态:
final FetchPostsRequest = () {
return (Store<AppState> store) {
final CollectionReference postCollection = Firestore.instance.collection("posts");
postCollection.getDocuments().then((QuerySnapshot snapshot) {
List<DocumentSnapshot> docs = snapshot.documents;
print("Getting documents....");
List<Model.Post> posts = docs.map((d) => Model.Post.fromMap(d.data)).toList();
store.dispatch(new FetchPostsSuccess(posts: posts));
}).catchError((err) {
print(err);
store.dispatch(new FetchPostsFailure(error: err));
});
};
};
如何将其用作RefreshIndicator's回调?它的回调是Future<Null> RefreshCallback ()
形式,我不知道如何纠正这个问题以适应redux状态管理思维。
答案 0 :(得分:0)
要在Flutter中将RefreshIndicator
与redux一起使用,应将Completer<Null>
放入Action类。
该计划是
Completer<Null>
action.completer.future
返回到onRefresh
的{{1}}功能让我们假设您有RefreshIndicator
。代码如下:
RefreshItemsAction
比中间件如下:
class RefreshItemsAction {
final Completer<Null> completer;
RefreshItemsAction({Completer completer})
: this.completer = completer ?? Completer<Null>();
}
Presentation层如下:
List<Middleware<AppState>> createItemsMiddleware() {
return [
TypedMiddleware<AppState, RefreshItemsAction>(_refreshItems),
];
}
Middleware<AppState> _refreshItems() {
return (Store<AppState> store, action, NextDispatcher next) {
if (action is RefreshItemsAction) {
loadItems().then((items) {
store.dispatch(ItemsRefreshedAction(items));
action.completer.complete();
},
).catchError((_) {
store.dispatch(ItemsNotRefreshedAction());
action.completer.complete();
});
}
next(action);
};
}
有关更多信息,请检查issue on github