1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Flutter] ModalRoute.withName never returns true in pushNamedAndRemoveUntil

Discussão em 'Mobile' iniciado por Stack, Outubro 10, 2024 às 05:33.

  1. Stack

    Stack Membro Participativo

    I have been wrestling with this issue for a couple of days now and I haven't been able to find a solution, hence the post. At one point in my app, I need to pop 2 routes from the navigator stack and push a new route. After researching the best way to do this, I have found that using pushNamedAndRemoveUntil is the best way, as I can specify ModalRoute.withName('/<route_name>') and it will pop the routes until it reaches /<route_name> at which point it will stop and push the new route. This is the line I have been using Navigator.of(context).pushNamedAndRemoveUntil('/raceadmin_reporting', ModalRoute.withName('/raceadmin_page'));.

    My issue though is that it doesn't work for me. It doesn't seem to matter what I put in /<route_name>, pushNamedAndRemoveUntil pops all the routes, which leads me to believe that ModalRoute.withName never returns true.

    I have also tried Navigator.of(context).pushNamedAndRemoveUntil('/raceadmin_reporting', (route) => route == RaceAdminPage.route()); and it doesn't work either.

    When I look at the debugger, this is what I see: App Navigator Stack

    The route I am trying to pop until is the RaceAdmin page, which is clearly in the stack. In the definition of that class, I added the line static const routeName = '/raceadmin_page'; which is what I call in ModalRoute.withName('/raceadmin_name') and it doesn't work.

    My routes are defined in the routes.dart file as per below:

    static Route<dynamic> generateRoute(RouteSettings settings) {
    switch (settings.name) {
    case '/':
    return MaterialPageRoute(builder: (context) => RailMeatApp());
    case '/raceadmin_page':
    return MaterialPageRoute(builder: (context) => const RaceAdminPage());
    case '/raceadmin_pendinglist':
    return MaterialPageRoute(builder: (context) => PendingResultsList());
    case '/raceadmin_pendingresults':
    final args = settings.arguments as Map<String?, String?>;
    return MaterialPageRoute(
    builder: (context) => PendingResultsPage(
    raceId: args['raceId'] as String,
    ));
    case '/raceadmin_reporting':
    return MaterialPageRoute(builder: (context) => RaceAdminReporting());
    default:
    return _errorRoute();
    }
    }


    And my MaterialApp is defined as per below:

    return MaterialApp(
    navigatorKey: _navigatorKey,
    home: _railmeatHome(),
    onGenerateRoute: RailmeatRoutes.generateRoute,
    );
    }


    In the Flutter debugger, I can look at the MaterialApp widget and I can see in its state that the navigator has 4 entries in its _history property as shown below: MaterialApp Widget Properties

    If I click on any of the entries in the _history, I see the the same info as below: Route property in _history

    As you can see, the name property under _settings is null, which, in my mind, would explain why ModalRoute.withName can't find the right route, but I am not sure that my thinking is accurate.

    What should I do differently to make pushNamedAndRemoveUntil work?

    Thanks a lot in advance, Bertrand.

    Continue reading...

Compartilhe esta Página