【问题标题】:A global key was used multiple times inside one widgets child list在一个小部件子列表中多次使用全局键
【发布时间】:2019-12-31 13:33:22
【问题描述】:

关闭应用后,当我尝试再次打开时,出现以下错误,但仅在 iOS 平台上,Android 运行良好。

我环顾四周,有几个关于这个问题的 SO 问题和问题,但我无法解决它。我也在使用bloc 模式来管理状态。

我的AuthenticateForm 中有GlobalKey<FormState>

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:low_code/blocs/authentication/authentication_bloc.dart';
import 'package:low_code/blocs/authentication/authentication_event.dart';
import 'package:low_code/helpers/app_localization/app_localizations.dart';

class AuthenticateForm extends StatefulWidget {
  @override
  _AuthenticateFormState createState() => _AuthenticateFormState();
}

class _AuthenticateFormState extends State<AuthenticateForm> {
  GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  String username;

  String password;

  @override
  Widget build(BuildContext context) {
    AppLocalizations appLocalizations = AppLocalizations.of(context);
    return Form(
      key: _formKey,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: <Widget>[
          TextFormField(
            onSaved: (String value) => username = value,
            initialValue: 'dms-bpm',
            decoration: InputDecoration(
                labelText: appLocalizations.translate('username')),
            // ignore: missing_return
            validator: (String value) {
              if (value.isEmpty) {
                return 'Please enter your ${appLocalizations.translate('username')}';
              }
            },
          ),
          TextFormField(
            onSaved: (String value) => password = value,
            obscureText: true,
            decoration: InputDecoration(
                labelText: appLocalizations.translate('password')),
            initialValue: 'dms-bpm',
            // ignore: missing_return
            validator: (String value) {
              if (value.isEmpty) {
                return 'Please enter your ${appLocalizations.translate('password')}';
              }
            },
          ),
          RaisedButton(
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(15))),
            child: Text(appLocalizations.translate('login')),
            onPressed: () {
              _formKey.currentState.save();
              if (_formKey.currentState.validate()) {
                BlocProvider.of<AuthenticationBloc>(context)
                  ..dispatch(
                      Authenticate(username: username, password: password));
              }
            },
          )
        ],
      ),
    );
  }
}

Flutter(Channel stable,v1.7.8+hotfix.4,在 Microsoft Windows [版本 10.0.17763.615],语言环境 en-GB)

【问题讨论】:

  • 据我所知,每个表单应该只有一个孩子
  • 尝试在 ListView、GridView SingleChildScrollView 等上添加键。这将解决您对两个滚动小部件使用相同键的问题
  • 我添加了所有ListViewGridViewSingleChildScrollView 的密钥,但它没有解决它。 @ArunR.Prajapati
  • @MuratAslan 所以你说:“每个表单应该只有一个 textformfield 小部件”。情况并非如此,表格的工作方式也并非如此。请查看 Flutter 的一些表单示例medium.com/flutter-community/…
  • 热重启。大多数错误是由于所做的更改

标签: flutter dart bloc


【解决方案1】:

就我而言,发生此错误是因为我设置了 initialRoute 属性,而我应该设置 home 属性:

class RouteTestApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Demo',
      //initialRoute: '/', //remove this
      home: FirstScreen(),  //this is the calling screen
      routes: {
        ExtractArgumentsScreen.routeName: (context) => ExtractArgumentsScreen(),
      },
    );
  }
}

【讨论】:

    【解决方案2】:

    尝试停止并重新运行您的应用程序,而不是热重新加载。这解决了我的问题。

    【讨论】:

    • 这不是一个解决方案。应用必须需要热重载或重启
    【解决方案3】:

    在我的情况下,我运行颤振应用程序而不添加 home: BottomNavBar()。 收到错误后,我添加了home: BottomNavBar() 并提供了热重载(Command + s),它重现了这个错误(一个全局键在一个小部件子列表中多次使用)。 然后我只是重新连接我的目标设备并再次运行,这解决了我的问题。

    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          theme: ThemeData(
            brightness:Brightness.light,
            primaryColor: Colors.white,
            accentColor: Colors.blue,
            fontFamily: 'Poppins',
            appBarTheme: AppBarTheme(
              centerTitle: true,
            ),
          ),
          home: BottomNavBar(),
        );
      }
    }
    

    【讨论】:

      【解决方案4】:

      我刚刚停止了热重载并重新启动了它为我工作的应用程序

      【讨论】:

      【解决方案5】:

      尝试将您的 GlobalKey 设为“最终”。我认为您的全局密钥是可变的这一事实可能会造成这个问题。

      【讨论】:

      • 它不会影响代码。但显然需要声明为final。
      【解决方案6】:

      在我的例子中,MaterialApp() 对象的 home: 属性中的调用屏幕返回 null。修复后,我停止了应用程序并重新运行它。

      【讨论】:

        猜你喜欢
        • 2020-06-02
        • 2020-11-29
        • 1970-01-01
        • 1970-01-01
        • 2021-04-03
        • 1970-01-01
        • 1970-01-01
        • 2021-12-14
        • 2021-06-01
        相关资源
        最近更新 更多