gpt4 book ai didi

Flutter 导航键 + GlobalKey : "method ' pushedName' was called on null"

转载 作者:行者123 更新时间:2023-12-01 05:52:23 25 4
gpt4 key购买 nike

我找到了 "GlobalKey NavigatorState" solution for middleware here但无法克服错误“方法 'pushedNamed' 被调用为 null”。然后我找到了这个 alternative solution to fix the issue使用单独的类和静态变量,但它产生了相同的错误。 我真正需要的只是一个非常基本的例子 设置以及如何在其他地方调用“pushedNamed”函数。

当前代码

在单独的文件夹中初始化 GlobalKey:

class NavKey{
static final navKey = new GlobalKey<NavigatorState>();
}

主要.dart:
import 'package:eumag/assets/tools/route_navigation_constants.dart';

...

@override
Widget build(BuildContext context) {
return StoreProvider<AppState>(
store: store,
child: MaterialApp(
theme: ThemeData.dark(),
navigatorKey: NavKey.navKey,
routes: <String, WidgetBuilder>{
ROUTE_LOGIN_PAGE: (BuildContext context) => LoginPage(store),
ROUTE_MAIN_PAGE: (BuildContext context) => MainPage(store),
},
home: StoreBuilder<AppState>(
builder: (BuildContext context, Store<AppState> store) =>
LoginPage(store),
),
),
);
}

中间件.dart:
import 'package:eumag/assets/tools/route_navigation_constants.dart';

void appStateMiddleware (Store<AppState> store, action, NextDispatcher next) async{

final navigatorKey = NavKey.navKey;

if (action is UpdateRoomStatus){
navigatorKey.currentState.pushNamed(ROUTE_MAIN_PAGE);
store.dispatch(createSocketConnection);
}
next(action);
}

最佳答案

有同样的问题,文档中的最后一行似乎给出了修复的提示。

navigatorKey property

你显然不能使用 routesnavigatorKey并且必须使用 onGenerateRoute .

关于Flutter 导航键 + GlobalKey : "method ' pushedName' was called on null",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52821745/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com