首页 > 解决方案 > 在一个小部件子列表中多次使用全局键

问题描述

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

在此处输入图像描述

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

GlobalKey<FormState>在我的AuthenticateForm.

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)

标签: flutterdartbloc

解决方案


就我而言,发生此错误是因为我设置了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(),
      },
    );
  }
}

推荐阅读