Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed assertion when using onGenerateRoute with Flutter back button

I am implementing navigation in my flutter app using onGenerateRoute in MaterialApp.

For one of the routes, I am getting this error when I press the backbutton Flutter provides in Appbar.

E/flutter (22996): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Failed assertion: boolean expression must not be null

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

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

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

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

Can you please help me understand what is wrong ?

My Route settings ->

  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;
    }
  }

Back button works for all the routes expect when I navigate back from the following route:

Navigator.of(context).pushNamed('/ChatroomFormView');
like image 264
vzurd Avatar asked Jun 26 '26 19:06

vzurd


1 Answers

Did you use WillPopScope widget? if so you need to provide a Function that return Future<boolean> to onWillPop parameter.

WillPopScope(
      onWillPop: () { 
        //need to return Future<bool> here
      }
}
like image 136
SoftwareEngineerCY Avatar answered Jun 29 '26 10:06

SoftwareEngineerCY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!