在我使用Redux架构的Flutter上的聊天应用程序中,我需要在一些异步调用的结果后显示对话框。我的主要问题是为show对话框获取当前的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();
}