AlertDialog中的错误

时间:2018-12-20 13:59:10

标签: flutter flutter-layout

我遵循https://codingwithjoe.com/flutter-navigation-how-to-prevent-navigation/进行Show AlertDialog,但是有两个错误:

  1. 从对话框中轻按时(不单击任何按钮即可关闭屏障)会出现运行时错误:
  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<…>

谢谢!

1 个答案:

答案 0 :(得分:2)

设置方法async并使用await关键字:

Future<bool> _exitApp(BuildContext context) async {
  return await showDialog(  ...

如果您不想在点击容器外部时关闭对话框,请将barrierDismissible设置为false。

showDialog(barrierDismissible: false, ...