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?

6
  • Without using "!" it gives compile time error, so it will not work.
    – Divyesh_08
    Jul 24, 2021 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) Jul 24, 2021 at 13:03
  • This will not make any difference.
    – Divyesh_08
    Jul 24, 2021 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? Jul 24, 2021 at 13:22
  • Thanks @ZeeshanAhmad File? _userImageFile = File(''); this worked for me.
    – Divyesh_08
    Jul 24, 2021 at 13:51

1 Answer 1

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('');
3
  • If you use _userImageFile = File(''), then _userImageFile should use a non-nullable type.
    – jamesdlin
    Jul 24, 2021 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, 2022 at 10:08
  • I can understand your pain, but we have to follow along.
    – Divyesh_08
    May 12, 2022 at 1:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.