如何解决如何在颤振中处理文件的空安全?
我正在使用 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}}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。