gpt4 book ai didi

android - Flutter with Redux : How to show alert from middleware? 我在哪里可以获得 BuildContext

转载 作者:IT王子 更新时间:2023-10-29 06:40:02 24 4
gpt4 key购买 nike

在我使用 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});
}
  • 在您的屏幕中,在 StoreConnector 中发送这些操作,例如
@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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com