问题描述
我正在使用 Firebase 并且我有 submitForm
这样的方法:
// File variable declaration
File? _userImageFile;
void _submitForm() {
try {
final isVaild = _formKey.currentState!.validate();
// To close soft keyboard
FocusScope.of(context).unfocus();
if (_userImageFile == null && !_isLogIn) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Please pick an image'),backgroundColor: Colors.black,),);
return;
}
if (isVaild) {
_formKey.currentState!.save();
widget._submitAuthForm(
_userName.trim(),_userImageFile!,_userEmail.trim(),_userPassword.trim(),_isLogIn,);
}
} catch (e) {
print(e.toString());
}
}
我已经使用相同的表单处理过注册和登录,所以当我登录时 _userImageFile
的值为空,所以它给出了异常:Null check operator used on a null value
在这里我不知道如何在颤振中处理这个问题?
解决方法
花了几个小时我得到的解决方案很少,我希望这也能帮助其他人:
- 通过编辑 pubspec.yml
File? _userImageFile = File('');
删除空安全
- 为登录和注册创建 2 个不同的函数,而不是单个 SubmitForm()
- 正如@ZeeshanAhmad 建议的那样{{1}}