问题描述
我是Flutter的新手,但遇到了问题。
我正在使用mobx。在我的视图中,我有一个按钮,并且在此按钮的内部,
我正在等待showDialog属性更改以显示对话框视图。但是,在onpress内,showdialog不起作用。还有其他方法吗?
我的控制器
@observable
bool showDialog = false;
@action
Future callLoginService() async {
await Future.delayed(Duration(seconds: 6));
showDialog = true;
}
视图
Observer(
builder: (_) {
return Center(
child: RaisedButton(
child: Text("TESTE"),onpressed: () async {
controller.callLoginService();
if (controller.showDialog) {
final action = await InfoDialogView.showAlertDialog(
context,"Try again",'Invalid user');
if (action == DialogAction.abort) {
controller.showDialog = false;
}
}
},),);
},
解决方法
这是因为您的 onPressed 方法是异步的,但您没有在 controller.callLoginService() 之前使用 'await' 关键字。
Observer(
builder: (_) {
return Center(
child: RaisedButton(
child: Text("TESTE"),onPressed: () async {
await controller.callLoginService(); //put await for calling asynchronous methods
if (controller.showDialog) {
final action = await InfoDialogView.showAlertDialog(
context,"Try again",'Invalid user');
if (action == DialogAction.abort) {
controller.showDialog = false;
}
}
},),);
},