【问题标题】:Flutter Duplicate GlobalKey detected in widget tree在小部件树中检测到 Flutter Duplicate GlobalKey
【发布时间】:2021-07-30 23:07:33
【问题描述】:

我在切换到另一个页面(从登录到仪表板页面)时遇到这个问题。

错误:

在小部件树中检测到重复的 GlobalKey。

... 由于 GlobalKey reparenting 导致一个或多个孩子被强制移除后未更新的特定 parent 是:

  • 扩展(弹性:1)

代码:

class LoginPage extends StatelessWidget {
  final GlobalKey<ScaffoldState> _key = GlobalKey<ScaffoldState>();
  final FocusNode passFocus = FocusNode();
  final FocusNode emailFocus = FocusNode();

  @override
  Widget build(BuildContext context) {
    final authProvider = Provider.of<AuthProvider>(context);

    return authProvider.status == Status.Authenticating
        ? Loading()
        : Scaffold(
          key: _key,
            body: ...

class LayoutTemplate extends StatelessWidget {
  final GlobalKey<ScaffoldState> _key = GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _key,
      body: ...

这是我的导航服务:

class NavigationService {
  final GlobalKey<NavigatorState> navigatorKey =
  GlobalKey<NavigatorState>();

  Future<dynamic> navigateTo(String routeName, {ArgumentsClass arguments}) {
    return navigatorKey.currentState.pushNamed(routeName, arguments: arguments);
  }

  Future<dynamic> globalNavigateTo(String routeName, BuildContext context) {
    return Navigator.of(context).pushNamed(routeName);
  }

  void goBack() {
    return navigatorKey.currentState.pop();
  }
}

【问题讨论】:

    标签: flutter flutter-layout flutter-web


    【解决方案1】:

    您已在导航服务中创建了一个密钥并将其发送到任何地方 脚手架键是一种独特的东西,您必须创建并仅在一个特定页面中使用它

    【讨论】:

    • 在 LayoutTemplate 类中,我有 Navigator 子项:child:Navigator(key: locator().navigatorKey, onGenerateRoute: generateRoute, initialRoute: HomeRoute, ),
    猜你喜欢
    • 2020-04-14
    • 2021-05-17
    • 2018-08-28
    • 2019-04-28
    • 2021-06-25
    • 2022-12-18
    • 2021-09-01
    • 2020-12-31
    • 2022-08-23
    相关资源
    最近更新 更多