这太荒谬了:D 一种形式工作正常,另一种形式具有完全相同的 textFormFields,但不同的 formsKey 和不同的方法会产生错误:
W/IInputConnectionWrapper(27244): getTextBeforeCursor on inactive
InputConnection
W/IInputConnectionWrapper(27244): getTextAfterCursor on inactive
InputConnection
W/IInputConnectionWrapper(27244): getSelectedText on inactive
InputConnection
W/IInputConnectionWrapper(27244): getTextBeforeCursor on inactive
InputConnection
W/IInputConnectionWrapper(27244): getTextAfterCursor on inactive
InputConnection
W/IInputConnectionWrapper(27244): getSelectedText on inactive
InputConnection
I/flutter (27244): Null check operator used on a null value
我的颤振医生:
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.8.1, on Microsoft Windows [Version
10.0.22000.434], locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK
version 31.0.0-rc5)
[√] Chrome - develop for the web
[√] Visual Studio - develop for Windows (Visual Studio Community 2019
16.10.2)
[√] Android Studio (version 2020.3)
[√] Connected device (4 available)
• 未发现任何问题!
工作表格用于登录:
buildForm(BuildContext context, LoginViewModel viewModel) {
return Form(
key: viewModel.formsKey,
child: Center(child:
Column(
children: [
const SizedBox(
height: 24,
),
const SizedBox(height: 15.0),
TextFormField(
autofocus: true,
enabled: !viewModel.loading,
decoration: const InputDecoration(
focusedBorder: OutlineInputBorder(borderSide:
BorderSide(color: Color(0xffdc9b7d))),
icon: Icon(Icons.email_outlined, color: Color(0xffdc9b7d)),
focusColor: Color(0xffdc9b7d), fillColor: Color(0xffdc9b7d),
),
textInputAction: TextInputAction.next,
validator: validateEmail,
onEditingComplete: (){
setState(() {
email = emailController.text.trim();
viewModel.setEmail(email);
});
},
focusNode: viewModel.emailFN,
controller: emailController,
keyboardType: TextInputType.emailAddress,
),
const SizedBox(height: 15.0),
TextFormField(
autofocus: true,
enabled: !viewModel.loading,
decoration: const InputDecoration(
focusedBorder: OutlineInputBorder(borderSide:
BorderSide(color: Color(0xffdc9b7d))),
icon: Icon(Icons.password_outlined, color:
Color(0xffdc9b7d)),
focusColor: Color(0xffdc9b7d), fillColor:
Color(0xffdc9b7d),
),
textInputAction: TextInputAction.done,
validator: validatePassword,
onFieldSubmitted: (String val) => viewModel.login(context),
obscureText: true,
onEditingComplete: (){
password = passController.text.trim();
viewModel.setPassword(password);
},
focusNode: viewModel.passFN,
controller: passController,
),
Align(
alignment: Alignment.centerRight,
child: Padding(
padding: const EdgeInsets.only(right: 10.0, top: 10),
child: InkWell(
onTap: () => viewModel.forgotPassword(context),
child: Text(
Languages.of(context)!.passwordForgot,
style: const TextStyle(
color: Colors.red,
fontWeight: FontWeight.bold,
),
),
),
),
),
const SizedBox(height: 50.0),
SizedBox(
height: 45.0,
width: 180.0,
child: ElevatedButton(
style: Constants.style,
child: Text(
Languages.of(context)!.login.toUpperCase(),
style: const TextStyle(
fontSize: 12.0,
fontWeight: FontWeight.w600,
),
),
onPressed: () async {
viewModel.login(context);
}
),
),
]),
));
}
它验证并登录用户,它会在 inputInactive 上触发警告,但仍然有效!
不起作用并触发 Null 值的表单是编辑个人数据表单:
buildForm(EditProfileViewModel viewModel, BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 20.0),
child: Form(
key: viewModel.formsKey1,
autovalidateMode: AutovalidateMode.always,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: [
const SizedBox(
height: 15.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
const Icon(Icons.account_circle_outlined,
color: Color(0xffdc9b7d)),
const SizedBox(width: 10),
Expanded(
child: TextFormField(
cursorColor: Constants.mainColor,
autofocus: true,
enabled: !viewModel.loading,
decoration: InputDecoration(
focusedBorder: const OutlineInputBorder(
borderSide: BorderSide(color: Color(0xffdc9b7d))),
focusColor: const Color(0xffdc9b7d),
fillColor: const Color(0xffdc9b7d),
hintText: Languages.of(context)!.username),
focusNode: viewModel.usernameFN,
controller: usernameController,
keyboardType: TextInputType.text,
validator: validateName,
textInputAction: TextInputAction.done,
onEditingComplete: (){
setState(() {
username = usernameController.text.trim();
viewModel.setUsername(username);
});
},
))],
),
const SizedBox(
height: 10.0,
),
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
const Icon(Icons.email_outlined,
color: Color(0xffdc9b7d)),
const SizedBox(width: 10),
Expanded(
child: TextFormField(
autofocus: true,
cursorColor: Constants.mainColor,
enabled: !viewModel.loading,
decoration: InputDecoration(
focusedBorder: const OutlineInputBorder(
borderSide: BorderSide(color: Color(0xffdc9b7d))),
focusColor: const Color(0xffdc9b7d),
fillColor: const Color(0xffdc9b7d),
hintText: Languages.of(context)!.email),
focusNode: viewModel.emailFN,
validator: validateEmail,
keyboardType: TextInputType.emailAddress,
textInputAction: TextInputAction.done,
controller: emailController,
onFieldSubmitted: (String val) => viewModel.editProfile(context),
onEditingComplete: (){
setState(() {
email=emailController.text.trim() ;
viewModel.setEmail(email);
});
},
)
)
]),
const SizedBox(height: 20.0),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget> [
ElevatedButton(
style: Constants.style,
onPressed: () async {
viewModel.editProfile(context);
},
child: Center(
child: Text(
Languages.of(context)!.save,
style: const TextStyle(color: Colors.white),
),
),)
]
),
],
),
),
);
}
对于身份验证和用户配置文件,我使用 firestore。我正在真实设备摩托罗拉 p30 笔记上进行测试。
我什么都试过了!不同的 onSaved、onChange 和 onEditingComplete 方法。仍然返回 null。
更新配置文件方法是:
editProfile(BuildContext context) async {
FormState? form = formsKey1.currentState;
if (form!=null) {
form.save();
if (!form.validate()) {
validate = true;
notifyListeners();
showInSnackBar(context, Languages.of(context)!.errorEditPro);
} else {
try {
loading = true;
notifyListeners();
bool success = await userService.updateProfile(
file: image!,
username: username,
email: email);
if (success) {
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (_) => const InputUserData()));
}
} catch (e) {
loading = false;
notifyListeners();
debugPrint(e.toString());
}
loading = false;
notifyListeners();
}}
}
接着:
updateProfile({
required Uint8List file,
required String username,
required String email
}) async {
String res = "Some error Occurred";
try {
String photoUrl =
await uploadImage('profilePic', file, false);
UserModel _user = UserModel(
username: username,
id: currentUserId(),
photoUrl: photoUrl,
email: email
);
await usersRef
.doc(currentUserId())
.update(_user.toJson());
res = "success";
} catch (err) {
return err.toString();
}
return res;
}
请帮忙!我不知道问题出在哪里,该怎么办?卡在这里好几天了!!谢谢!