gpt4 book ai didi

flutter - 在一个小部件子列表中多次使用全局键

转载 作者:行者123 更新时间:2023-12-03 03:05:07 25 4
gpt4 key购买 nike

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

enter image description here

我环顾四周,有几个关于这个问题的问题和问题,但我无法解决它。我还使用 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, on Microsoft Windows [Version 10.0.17763.615], locale 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(),
},
);
}
}

关于flutter - 在一个小部件子列表中多次使用全局键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57673489/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com