如何从提供商处设置导航键?我尝试了key: Provider.of<RoutesProvider>(context).setHomeKey()
,但收到一条错误消息:
此表达式的类型为“ void”,因此无法使用其值。
这是我的代码:
class RoutesProvider extends ChangeNotifier {
GlobalKey _homeKey = GlobalKey();
get homeKey => _homeKey;
void setHomeKey(GlobalKey newKey) {
_homeKey = newKey;
notifyListeners();
}
}
Navigator(
key: Provider.of<RoutesProvider>(context).setHomeKey()
onGenerateRoute: (settings) {
return MaterialPageRoute(
settings: settings,
builder: (context) {
switch (settings.name) {
case '/':
return Home();
default:
throw Exception('Invalid route: ${settings.name}');
}
},
);
},
);
我要推到新路线时遇到的新错误:
Exception: Invalid route: /someRouteInHome
The relevant error-causing widget was
Navigator-[GlobalKey#88e11]