作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
在我使用 Redux 架构的 Flutter 聊天应用程序中,我需要在某些异步调用的结果后显示对话框。我的主要问题是获取显示对话框的当前 BuildContext。这个异步调用可以从不同的屏幕完成,我需要当前屏幕的上下文。
我在中间件端的调用如下所示:
void _setCompanionToChat(String groupChatId) {
var documentReference = _getChatDocument(groupChatId);
documentReference.get().then((snapshot) {
var closed = snapshot[ChatDatabase.CLOSED_ATTRIBUTE];
if (snapshot.exists && !closed) {
// SOME OPERATIONS
} else {
// SHOW DIALOG
}
});
}
有什么建议如何做到这一点?
最佳答案
也许您可以将对话作为回调传递:
class MyDataRequestAction {
...
Function onError;
MyDataRequestAction({this.onError});
}
@override
Widget build(BuildContext context) {
...
StoreConnector<AppState, _MyScreenViewModel>(
onInit: (store) => store.dispatch(MyDataRequestAction(
onError: () => showDialog(context: context, builder: (context) => AlertDialog(...));
));
...
class MyMiddleware extends MiddlewareClass<AppState> {
...
@override
void call(Store<AppState> store, action, NextDispatcher next) async {
...
_setCompanionToChat(groupChatId, action) // PASS THE ACTION!!
}
void _setCompanionToChat(String groupChatId, dynamic action) {
...
if (snapshot.exists && !closed) {
// SOME OPERATIONS
} else {
action.onError();
}
关于android - Flutter with Redux : How to show alert from middleware? 我在哪里可以获得 BuildContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54947433/
我是一名优秀的程序员,十分优秀!