【问题标题】:Flutter 2.0 pushAndRemoveUntil not workingFlutter 2.0 pushAndRemoveUntil 不工作
【发布时间】:2021-08-19 07:55:53
【问题描述】:

不确定如何在实现 Flutter 2.0 路由后清除堆栈并路由到新页面。

以下内容不起作用:

Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(builder: (context) => SignInPage()), (route) => false);

[VERBOSE-2:ui_dart_state.cc(186)] Unhandled Exception: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 3075 pos 7: '!hasPage || isWaitingForExitingDecision': A page-based route cannot be completed using imperative api, provide a new list without the corresponding Page to Navigator.pages instead.

【问题讨论】:

  • 等等,你正在使用MaterialApp.routerNavigator.of(context).pushAndRemoveUntil
  • 我是。你知道如何使用 MaterialApp.router 完成这种类型的行为吗?它本质上是一个注销功能 -> 将用户带到登录屏幕,不要让他们“返回”应用程序。
  • 错误提示如果您使用的是MaterialApp.router,那么您不能使用pushAndRemoveUntil(“导航器的命令式api”)——相反,您需要使用路由器的api,更多:medium.com/flutter/…
  • 听起来没有一对一的替代方案,您需要在路由类中添加一个可以在整个应用程序中拉入的 ChangeNotifier。然后调用一个方法来处理整个路由器的状态更新。 class RoutePageManager extends ChangeNotifier { static RoutePageManager of(BuildContext context) { return Provider.of<RoutePageManager>(context, listen: false); } ...

标签: flutter dart flutter-navigation


【解决方案1】:

您可以使用 rootNavigator: true 访问您的 MaterialApp 导航器。见https://api.flutter.dev/flutter/widgets/Navigator/of.html。尝试以下操作。

Navigator.of(context, rootNavigator: 
true).pushAndRemoveUntil(MaterialPageRoute(builder: (context) => 
SignInPage()), (route) => false);

【讨论】:

    【解决方案2】:

    我认为您还需要传递根名称。

    检查我的代码,希望对你有帮助

    Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (BuildContext context) => SignInPage()), ModalRoute.withName('/'),
    

    【讨论】:

    • 同样的错误:Unhandled Exception: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 3075 pos 7: '!hasPage || isWaitingForExitingDecision': A page-based route cannot be completed using imperative api, provide a new list without the corresponding Page to Navigator.pages instead.
    【解决方案3】:

    要完全移除堆栈,使用户无法导航到上一个屏幕,您应该使用PushReplacement

    Navigator.of(context).pushReplacement(
      MaterialPageRoute(builder: (context) => SigInPage()),
    );
    

    【讨论】:

    • 这确实将屏幕移动到 SignInPage 但仍让用户导航回他们所在的位置。我可以通过隐藏后退按钮来破解防止后退导航的方法,但这听起来不是最好的方法。
    【解决方案4】:

    试试这个

    Navigator.pushAndRemoveUntil(
        context, MaterialPageRoute(builder: (context) => SignInPage()), (
        route) => false);
    

    【讨论】:

    • 同样的错误:Unhandled Exception: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 3075 pos 7: '!hasPage || isWaitingForExitingDecision': A page-based route cannot be completed using imperative api, provide a new list without the corresponding Page to Navigator.pages instead.
    猜你喜欢
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    • 2012-09-18
    相关资源
    最近更新 更多