flutter - 在一个小部件子列表中多次使用全局键
问题描述
关闭应用程序后,当我尝试再次打开它时,出现以下错误,但它仅在 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)
解决方案
就我而言,发生此错误是因为我设置了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(),
},
);
}
}
推荐阅读
- filter - 是否可以在 NestJs 上的异常过滤器上使用 useInterceptor?
- python - 与循环一起使用时,Ansible 覆盖安全组规则
- nexus - 将 jar 部署到服务器的 RPM 规范文件
- javascript - 如何通过点击事件使用新值更新变量并使用相同事件撤消它
- javascript - 将“异步”方法绑定到“开启”事件(例如单击)时是否存在任何潜在问题?
- r - R - 有没有办法在重复即将进行的观察后生成从 1 开始的行号?
- flask - 根据配置导入不同的sqlalchemy方言
- laravel - 我如何使用 Laravel Sanctum 同时处理 SPA 和基于令牌的身份验证
- c# - 与界面交互
不同实现列表上的方法 - php - PHP没有迭代