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