【问题标题】:User NavigatorState to clear all routes and push on home route用户 NavigatorState 清除所有路由并推送主路由
【发布时间】:2020-07-24 03:49:18
【问题描述】:

我在我的应用程序中使用Firebase messaging。在任何时候我都认为FirebaseAuth.onAuthStateChanged 如果登录状态发生变化,则可以调用回调。发生这种情况时,我想将应用重置回主(登录)屏幕。

为此,我使用以下代码:

// navigatorKey is of type GlobalKey<NavigatorState> because in the firebase callback I dont have a context
navigatorKey.currentState.popUntil((Route<dynamic> route) => false);
navigatorKey.currentState.pushNamed(NavBoss.ENTRY_ROUTE);

当发生登录时,我 pushReplacement 使主屏幕不再存在于路由堆栈中。所以我想弹出直到所有路线都消失了,然后重新打开主页(登录)屏幕。

但是当我执行这段代码时,我得到一个错误......

I/flutter (18716): #0 ListMixin.lastWhere (dart:collection/list.dart:163:5) I/flutter (18716): #1
NavigatorState.popUntil (包:flutter/src/widgets/navigator.dart:3620:32)

有没有办法可以使用 NavigatorState 清除堆栈上的所有路由?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    试试下面的代码

    import 'package:flutter/material.dart';
    
    /// A model which globally provides [NavigatorState]'s functionality.
    ///
    /// Even if a page is inside of nesting navigators, you can go outside
    /// of the nesting navigator to use outside [NavigatorState] by [AppNavigator].
    ///
    /// # Usage
    ///
    /// ```dart
    /// final appNavigator = Provider.of<AppNavigator>(context, listen:false);
    /// appNavigator.push(SamplePage.routeName);
    /// ```
    class AppNavigator {
      AppNavigator({
        @required this.navigatorKey,
      });
    
      final GlobalKey<NavigatorState> navigatorKey;
    
      /// [Navigator] which is held by [AppNavigator]
      NavigatorState get _navigator => navigatorKey.currentState;
    
      /// Pushes page by [routeName].
      ///
      /// This is same as [Navigator.pushNamed].
      void push(String routeName, {Object arguments}) =>
          _navigator.pushNamed(routeName, arguments: arguments);
    
      /// Pushes page by [routeName] and remove all pages.
      ///
      /// This is same as [Navigator.pushNamedAndRemoveUntil].
      void pushAndRemoveAllPage(String routeName, {Object arguments}) =>
          _navigator.pushNamedAndRemoveUntil(
            routeName,
            (route) => false,
            arguments: arguments,
          );
    
      /// Replaces current page and Pushes page by [routeName].
      ///
      /// This is same as [Navigator.pushReplacementNamed].
      void pushReplacement(String routeName, {Object arguments}) =>
          _navigator.pushReplacementNamed(routeName, arguments: arguments);
    
      /// Pops page if it can pop.
      ///
      /// This is same as [Navigator.maybePop].
      void pop() => _navigator.maybePop();
    }
    

    然后materialapp看起来像 使用提供者

    Provider(
              lazy: false,
              create: (_) => AppNavigator(
                navigatorKey: GlobalKey(debugLabel: "for Global Navigation"),
              ),
            ),
    

    终于在材料应用中

     return MaterialApp(
          navigatorKey:
              Provider.of<AppNavigator>(context, listen: false).navigatorKey,
          onGenerateRoute:
              Provider.of<GlobalRouter>(context, listen: false).onGenerateRoute,
          home: () {
            return const SplashPage();
          }(),
        );
    

    终于像这样使用了

    appNavigator.push(SamplePage.routeName);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-25
      • 2017-11-01
      • 2020-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多