flutterflutter-navigationrelease-mode

Flutter Navigator.popUntil() with ModalRoute.withName() not working in profile and release mode


I have a function that navigates to a new page with route settings

void pushWithSettings(
    {@required BuildContext context, @required Widget newPage}) {
  Navigator.push(
      context,
      MaterialPageRoute(
          settings: RouteSettings(
            name: newPage.toString(),
          ),
          builder: (_) => newPage));
}

and I pop till a particular page using

Navigator.popUntil(context, ModalRoute.withName(Page().toString));

This works well in debug mode but in profile and release mode it pops only once

An Example for context

import 'package:flutter/material.dart';
import 'package:flutter_app/main.dart';

class FirstPage extends StatelessWidget {
  
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Center(child: Text("First Page"),),
        RaisedButton(
          onPressed: (){
            pushWithSettings(context: context, newPage: SecondPage());
          },
          child: Text("Navigate to second", style: TextStyle(inherit: false)),
        )
      ],
    );
  }
}

class SecondPage extends StatelessWidget {
  
@override
Widget build(BuildContext context) {
  return Column(
    children: <Widget>[
      Center(child: Text("Second Page"),),
      RaisedButton(
        onPressed: (){
          pushWithSettings(context: context, newPage: ThirdPage());
        },
        child: Text("Navigate to third", style: TextStyle(inherit: false)),
      )
    ],
  );
}
}

class ThirdPage extends StatelessWidget {
  
  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Center(child: Text("Third Page"),),
        RaisedButton(
          onPressed: (){
            Navigator.popUntil(context, ModalRoute.withName(FirstPage().toString()));
          },
          child: Text("Navigate to first", style: TextStyle(inherit: false),),
        )
      ],
    );
  }
}

In debug mode tapping the button in the third page navigates to the first page but in profile and release mode tapping the button does nothing. I'm currently running flutter 1.17.4 on the stable channel


Solution

  • I have tried your code an FirstPage().toString() in Profile mode returns 'Widget' while it returns 'FirstPage' in debug.

    I would add a mixin that contains a routeName string you can use.

    Also remember, that at least for web is recommended that your initial route is with '/'.

    
    void pushWithSettings(
        {@required BuildContext context, @required NamedRoute newPage}) {
      Navigator.push(
          context,
          MaterialPageRoute(
              settings: RouteSettings(
                name: newPage.routeName,
              ),
              builder: (_) => newPage));
    }
    
    mixin NamedRoute implements Widget {
      String get routeName;
    }
    
    
    class ThirdPage extends StatelessWidget with NamedRoute {
      @override
      String get routeName => '/third_page';
    
      @override
      Widget build(BuildContext context) {
        return Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Center(
              child: Text("Third Page"),
            ),
            RaisedButton(
              onPressed: () {
                Navigator.popUntil(context, ModalRoute.withName(FirstPage().routeName));
              },
              child: Text(
                "Navigate to first",
                style: TextStyle(inherit: false),
              ),
            )
          ],
        );
      }
    }
    
    

    Check the codepen https://codepen.io/jamesblasco/pen/QWyBQOm