如何在颤振中处理文件的空安全?

时间:2021-07-24 11:34:39

标签: flutter dart flutter-test flutter-image flutter-null-safety

我正在使用 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

在这里我不知道如何在颤振中处理这个问题?

1 个答案:

答案 0 :(得分:0)

花了几个小时我得到的解决方案很少,我希望这也能帮助其他人:

  1. 通过编辑 pubspec.yml File? _userImageFile = File('');
  2. 删除空安全
  3. 为登录和注册创建 2 个不同的函数,而不是单个 SubmitForm()
  4. 正如@ZeeshanAhmad 建议的那样{{1}}
相关问题