flutter 未使用GlobalKey仍在获取GlobalKey被多次使用错误

iyzzxitl  于 2023-05-30  发布在  Flutter
关注(0)|答案(1)|浏览(270)

我正在尝试使用NavigatorMiddleWare
下面是我的主要.dart代码:

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
      .then((value) => runApp(const MyApp()));
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: Strings.appTitle,
      theme: AppTheme().light(),
      initialRoute: NavigationManager.returnNavigationRoute(),
      onGenerateRoute: RouteGenerator.generateRoute,
      navigatorObservers: [RouteManager.routeObserver, RouteManager.middleware],
    );
  }
}

我的路由管理器类:

class RouteManager {
  static final RouteObserver<PageRoute> routeObserver =
      RouteObserver<PageRoute>();

  static final NavigatorMiddleware<PageRoute> middleware =
      NavigatorMiddleware<PageRoute>(
    onPush: (route, previousRoute) {
      log('we have push event');
    },
  );
}

在我身边似乎一切都很好。tutorial
添加了NavigatorMiddleWare代码:NavigationMiddleWare

monwx1rj

monwx1rj1#

将您的中间件代码替换为以下代码:

import 'dart:developer'; 
 import 'package:flutter/material.dart'; 
  
 typedef OnRouteChange<R extends Route<dynamic>> = void Function(R route, R? previousRoute); 
  
 class NavigatorMiddleware<R extends Route<dynamic>> extends RouteObserver<R> { 
   NavigatorMiddleware({ 
     this.enableLogger = true, 
     this.onPush, 
     this.onPop, 
     this.onReplace, 
     this.onRemove, 
   }) : _stack = []; 
  
   final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>(); 
   final List<R> _stack; 
   final bool enableLogger; 
   final OnRouteChange<R>? onPush; 
   final OnRouteChange<R>? onPop; 
   final OnRouteChange<R>? onReplace; 
   final OnRouteChange<R>? onRemove; 
  
   List<R> get stack => List<R>.from(_stack); 
  
   @override 
   void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) { 
     _logger('{didPush} \n route: $route \n previousRoute: $previousRoute'); 
     _stack.add(route as R); 
     _logStack(); 
     if (onPush != null) { 
       onPush!(route, previousRoute as R?); 
     } 
     super.didPush(route, previousRoute); 
   } 
  
   @override 
   void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) { 
     _logger('{didPop} \n route: $route \n previousRoute: $previousRoute'); 
     _stack.remove(route); 
     _logStack(); 
     if (onPop != null) { 
       onPop!(route as R, previousRoute as R?); 
     } 
     super.didPop(route, previousRoute); 
   } 
  
   @override 
   void didReplace({Route<dynamic>? newRoute, Route<dynamic>? oldRoute}) { 
     _logger('{didReplace} \n newRoute: $newRoute \n oldRoute: $oldRoute'); 
     if (_stack.contains(oldRoute)) { 
       final oldItemIndex = _stack.indexOf(oldRoute as R); 
       _stack[oldItemIndex] = newRoute as R; 
     } 
     _logStack(); 
     if (onReplace != null) { 
       onReplace!(newRoute as R, oldRoute as R?); 
     } 
     super.didReplace(newRoute: newRoute, oldRoute: oldRoute); 
   } 
  
   @override 
   void didRemove(Route<dynamic> route, Route<dynamic>? previousRoute) { 
     _logger('{didRemove} \n route: $route \n previousRoute: $previousRoute'); 
     stack.remove(route); 
     _logStack(); 
     if (onRemove != null) { 
       onRemove!(route as R, previousRoute as R?); 
     } 
     super.didRemove(route, previousRoute); 
   } 
  
   @override 
   void didStartUserGesture(Route<dynamic> route, Route<dynamic>? previousRoute) { 
     _logger('{didStartUserGesture} \n route: $route \n previousRoute: $previousRoute'); 
     super.didStartUserGesture(route, previousRoute); 
   } 
  
   @override 
   void didStopUserGesture() { 
     _logger('{didStopUserGesture}'); 
     super.didStopUserGesture(); 
   } 
  
   void _logStack() { 
     final mappedStack = 
     _stack.map((Route route) => route.settings.name).toList(); 
  
     _logger('Navigator stack: $mappedStack'); 
   } 
  
   void _logger(String content) { 
     if (enableLogger) { 
       log(content); 
     } 
   } 
 }

希望有帮助

相关问题