0

I am working with Firebase and I have submitForm method like this:


// 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());
    }
  }

I have handled Signup & Login using same form, so when I am login value of _userImageFile will be null, so it gives exception: Null check operator used on a null value

Here I am not able to figure out how to handle this in flutter?

SuperStormer
  • 4,997
  • 5
  • 25
  • 35
Divyesh
  • 2,085
  • 20
  • 35
  • Without using "!" it gives compile time error, so it will not work. – Divyesh Jul 24 '21 at 12:50
  • Instead of using `if (_userImageFile == null && !_isLogIn)`, you should also check if file path is empty or not. Like that `if ((_userImageFile == null || _userImageFile.path.isEmpty) && !_isLogIn)` – Zeeshan Ahmad Jul 24 '21 at 13:03
  • This will not make any difference. – Divyesh Jul 24 '21 at 13:10
  • 1
    How about you make **_userImageFile** not nullable(`File? _userImageFile = File('');`), initialize it with an empty path, and update its path when the user picks an image? – Zeeshan Ahmad Jul 24 '21 at 13:22
  • Thanks @ZeeshanAhmad File? _userImageFile = File(''); this worked for me. – Divyesh Jul 24 '21 at 13:51
  • Was your `if (_userImageFile == null && !_isLogIn`) check meant to use `||` instead of `&&`? By using `&&`, your function will not exit early if `_userImageFile` is `null`. Or perhaps you intended your `if (isVaild)` (sic) check to be `if (isVaild && _userImageFile != null)`. – jamesdlin Jul 24 '21 at 17:06

1 Answers1

1

After spent couple of hours I have got few solutions, I hope this will help others too:

  1. Removing null safety by editing pubspec.yml sdk: ">=2.3.0 <3.0.0"
  2. Creating 2 different function for Login and Signup instead of single SubmitForm()
  3. As @ZeeshanAhmad suggested File? _userImageFile = File('');
Divyesh
  • 2,085
  • 20
  • 35
  • If you use `_userImageFile = File('')`, then `_userImageFile` should use a non-nullable type. – jamesdlin Jul 24 '21 at 17:12
  • I came here looking for a solution to a similar problem with File. Thanks for the idea, though this is a 'hack'. Empty path can lead to other errors, has to be checked etc etc. instead of "honest null" which is de facto industry standard and is dealt with in any other language without any problems. TBH, this null-safety that they dropped on us as a whole is such a pain in the behind for any nomral coder - I've been doing null checks in code for all my life anyway, duh. Now I have to cope with this "safety railing" which I never asked for. – galloper May 11 '22 at 10:08
  • I can understand your pain, but we have to follow along. – Divyesh May 12 '22 at 01:32