设置导航键会导致无效错误

时间:2020-09-18 02:11:03

标签: flutter

如何从提供商处设置导航键?我尝试了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] 

0 个答案:

没有答案