stacktrace2234
stacktrace2234

Reputation: 1500

Flutter: feels like my Navigator stack is empty, although if I use Navigator.pop it works well, black screen comes if I use Navigator.popUntil?

Edit: I ended up to make it work, changed the code.

This is how I start the app:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Test Demo',
        home: LoginPage(),
         onGenerateRoute: (settings) {
          return CustomPageRouteBuilder.getPageRouteBuilder(Routing.openRoute(settings), settings.name);
        },
    );
  }
}

class CustomPageRouteBuilder {
  static PageRouteBuilder getPageRouteBuilder(Widget widget, [String? name]) {
    return PageRouteBuilder(
        settings: RouteSettings(name: name),
        transitionDuration: Duration(
          milliseconds: 100,
        ),
        transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secAnimation, Widget child) {
          animation = CurvedAnimation(
            parent: animation,
            curve: Curves.elasticIn,
          );
          return FadeTransition(
            opacity: animation,
            child: child,
          );
        },
        pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secAnimation) {
          return Scaffold(
            body: Stack(children: [
              BiometricAuthentication(widget),
              Align(
                alignment: Alignment.bottomLeft,
                child: Opacity(
                  opacity: 0.5,
                  child: Text("Version: ${Constants.buildVersion}",
                      style: TextStyle(
                        color: Constants.FONT_COLOR,
                        fontSize: 12,
                      )),
                ),
              )
            ]),
          );
        });
  }
}

Then I let my Routing class handle the routing:

enum Routes {
  LOGIN,
  FIRST_PAGE,
  SECOND_PAGE,
  THIRD_PAGE,
}

extension GetRoute on Routes {
  String route() {
    return '/' + this.toString().split('.').last.toLowerCase().replaceAll('_', '-');
  }
}

class Routing {
  static openRoute(RouteSettings settings) {
    return getRoute(settings);
  }

  static getRoute(RouteSettings settings) {
     String? name = settings.name;
     if (name == Routes.LOGIN.route()) {
      return LoginPage();
    }

    if (name == Routes.FIRST_PAGE.route()) {
      return FirstPage();
    }

   if (name == Routes.SECOND_PAGE.route()) {
      return SecondPage();
   }

   if (name == Routes.THIRD_PAGE.route()) {
      return ThirdPage();
   }
  }
}

I get on my ThirdPage on the route of: Login --> FirstPage --> SecondPage --> ThirdPage using the code below:

await Navigator.pushNamed(context, Routes.FIRST_PAGE.route()); // Login
await Navigator.pushNamed(context, Routes.SECOND_PAGE.route()); // FirstPage
await Navigator.pushNamed(context, Routes.THIRD_PAGE.route()); // SecondPage

So I should have 4 pages in my Navigator stack.

Now I try to popUntil Routes.FIRST_PAGE.route(), so the Login and FirstPage should be in the navigator:

Navigator.popUntil(context, ModalRoute.withName(Routes.FIRST_PAGE.route())); // ThirdPage

Here I expect to have the Login -> FirstPage in the stack but everything goes black.

If I use simply the Navigator.pop(context), from ThirdPage it goes back to SecondPage. But in my actuall app I have to go back 5 screens in case a button is used so I'd prefer to not handle that in every page...

Any ideas? Thanks in advance.

Upvotes: 0

Views: 734

Answers (1)

Mozes Ong
Mozes Ong

Reputation: 1294

    switch (settings.name) {
      case RouteNames.home:
        return MaterialPageRoute(
          settings: RouteSettings(name: '/HomePage'),
          builder: (_) => HomePage(),
        );
     };

This is how I set up my pages and return them to onGenerateRoute . I suspect you did not set the route setting name settings: RouteSettings(name: '/HomePage'). This will cause the navigator to not be aware of your route name existing on the stack.

Upvotes: 1

Related Questions