Home > Back-end >  Unhandled Exception: Null check operator used on a null value Uint8List
Unhandled Exception: Null check operator used on a null value Uint8List

Time:07-25

Unhandled Exception: Null check operator used on a null value Friends, I have indicated the place where the error is below and I tried a few things, but I still could not get rid of this error. Does anyone know the solution? When I remove ! from _image!, I still get an error.

  Uint8List? _image;
    signUpUser() async {
            // set loading to true
            setState(() {
              _isLoading = true;
            });
                String res = await AuthMethods().signUpUser(
                email: _emailController.text,
                password: _passwordController.text,
                username: _usernameController.text,
                bio: _bioController.text,
                gender: _genderController.text,
                file: _image! //--> error);
            if (res == "success") {
              setState(() {
                _isLoading = false;
              });
              Navigator.push(context, MaterialPageRoute(builder: (context) => HomePage()));
            } else {
              setState(() {
                _isLoading = false;
              });
              // show the error
              showSnackBar(context, res);
            }
          }

selectImage() async {
    Uint8List im = await pickImage(ImageSource.gallery);
    setState(() {
      _image = im;
    });
  }

CodePudding user response:

The _image is null. I don't see where you call the selectImage() function, and other than that function _image is never defined. Even if selectImage() was called, when the picker appears the user may not actually pick an image and cancel it or another error can happen. The AuthMethods().signUpUser() method seems to not allow the file parameter to be null, that's why when you remove the ! it doesn't work.

  • Related