我遵循https://codingwithjoe.com/flutter-navigation-how-to-prevent-navigation/进行Show AlertDialog,但是有两个错误:
Dart Error: Unhandled exception:Failed assertion: boolean expression must not be null
2。 child:新的AlertDialog(“不赞成使用child,不应该使用它。”
代码:
Future<bool> _exitApp(BuildContext context) {
return showDialog(
context: context,
child: new AlertDialog(
title: new Text('Do you want to exit this application?'),
content: new Text('We hate to see you leave...'),
actions: <Widget>[
new FlatButton(
onPressed: () => Navigator.of(context).pop(false),
child: new Text('No'),
),
new FlatButton(
onPressed: () => Navigator.of(context).pop(true),
child: new Text('Yes'),
),
],
),
) ??
false;
}
Stacktrace:
#0 ModalRoute.willPop (package:flutter/src/widgets/routes.dart)
<asynchronous suspension>
#1 NavigatorState.maybePop (package:flutter/src/widgets/navigator.dart:1783:57)
<asynchronous suspension>
#2 Navigator.maybePop (package:flutter/src/widgets/navigator.dart:1133:34)
#3 BackButton.build.<anonymous closure> (package:flutter/src/material/back_button.dart:91:19)
#4 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:507:14)
#5 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:562:30)
#6 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
#7 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)
#8 TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:204:7)
#9 GestureArenaManager.sweep (package:flutter/src/g<…>
谢谢!
答案 0 :(得分:2)
设置方法async
并使用await
关键字:
Future<bool> _exitApp(BuildContext context) async {
return await showDialog( ...
如果您不想在点击容器外部时关闭对话框,请将barrierDismissible
设置为false。
showDialog(barrierDismissible: false, ...