将onGenerateRoute与Flutter后退按钮结合使用时断言失败

时间:2019-03-23 01:20:19

标签: dart flutter flutter-navigation

我正在使用MaterialApp中的onGenerateRoute在flutter应用程序中实现导航。

对于其中一条路线,当我按下Flutter在Appbar中提供的后退按钮时,出现此错误。

  

E / flutter(22996):[错误:flutter / lib / ui / ui_dart_state.cc(148)]   未处理的异常:断言失败:布尔表达式不能为   空

     

E / flutter(22996):#0 ModalRoute.willPop   (package:flutter / src / widgets / routes.dart)E / flutter(22996):   

     

E / flutter(22996):#1 NavigatorState.maybePop   (包:flutter / src / widgets / navigator.dart:1964:57)E / flutter   (22996):

     

E / flutter(22996):#2 Navigator.maybePop   (软件包:flutter / src / widgets / navigator.dart:1291:34)

     E / flutter(22996):#3 BackButton.build。   (package:flutter / src / material / back_button.dart:91:19)

能否请您帮助我了解问题所在?

我的路线设置->

  Route<dynamic> routes(RouteSettings settings) {
    switch (settings.name) {
      case '/':
        return MaterialPageRoute<dynamic>(
          builder: (BuildContext context) {
            return HomePage();
          },
        );
        break;
      case '/contactlist':
        return MaterialPageRoute<dynamic>(
          builder: (BuildContext context) {
            return FriendsList();
          },
        );
        break;
      case '/ChatroomFormAdd':
        return MaterialPageRoute<dynamic>(
          builder: (BuildContext context) {
            return const ChatroomInfo(
                mode: 'Add');
          },
        );
        break;
      case '/ChatroomFormEdit':
        return MaterialPageRoute<dynamic>(
          builder: (BuildContext context) {
            return const ChatroomInfo(
                mode: 'Edit');
          },
        );
        break;
      case '/ChatroomFormView':
        return MaterialPageRoute<dynamic>(
          builder: (BuildContext context) {
            return const ChatroomInfo(
                mode: 'View');
          },
        );
        break;
      case '/ChatroomFormApprove':
        return MaterialPageRoute<dynamic>(
          builder: (BuildContext context) {
            return const ChatroomInfo(
                mode: 'Approve');
          },
        );
        break;
      case '/errorscreen':
        return MaterialPageRoute<dynamic>(
          builder: (BuildContext context) {
            return ErrorScreen();
          },
        );
        break;
      default:
        return MaterialPageRoute<dynamic>(
          builder: (BuildContext context) {
            return ChatDetail(chatroomID: chatroomID);
          },
        );
        break;
    }
  }

“后退”按钮适用于我从以下路线导航回来的所有路线:

Navigator.of(context).pushNamed('/ChatroomFormView');

1 个答案:

答案 0 :(得分:0)

您是否使用过WillPopScope小部件?如果是这样,则需要提供一个将Future<boolean>返回到onWillPop参数的函数。

WillPopScope(
      onWillPop: () { 
        //need to return Future<bool> here
      }
}