Flutter Navigator.popUntil()和ModalRoute.withName()在配置文件和发布模式下不起作用

时间:2020-07-12 00:37:55

标签: flutter flutter-navigation release-mode

我具有使用路线设置导航到新页面的功能

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

然后我使用弹出直到特定页面

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

这在调试模式下效果很好,但在配置文件和发布模式下,它仅弹出一次

上下文示例

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),),
        )
      ],
    );
  }
}

在调试模式下,点击第三页中的按钮将导航至第一页,但是在配置文件和发行版模式中,点击该按钮则无效。 我目前正在稳定频道上运行flutter 1.17.4

2 个答案:

答案 0 :(得分:1)

我已经尝试过您的代码,在配置文件模式下的FirstPage().toString()返回'Widget',而在调试时返回'FirstPage'。

我将添加一个包含可以使用的routeName字符串的混合。

还请记住,至少对于Web,建议您的初始路由使用'/'。


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),
          ),
        )
      ],
    );
  }
}

检查代码笔https://codepen.io/jamesblasco/pen/QWyBQOm

答案 1 :(得分:0)

第一个答案有效,但为避免向所有可用页面添加混合,我改用runtimeType

settings: RouteSettings(
            name: newPage.toString(),
          ),

成为

settings: RouteSettings(
            name: newPage.runtimeType.toString(),
          ),