Home > Enterprise >  type 'Null' is not a subtype of type 'File' of 'function result' in fl
type 'Null' is not a subtype of type 'File' of 'function result' in fl

Time:12-17

I am working on flutter to upload images to the SQL server but there is a error :

type 'Null' is not a subtype of type 'File' of 'function result'

here is how I am initialising the file:

late File _image;

final picker=ImagePicker();

Future choiceImage() async{
 var pickedImage= await picker.getImage(source: ImageSource.gallery);
 setState(() {

  _image=File(pickedImage!.path);
});
}
 Future choice2Image() async{
   var pickedImage= await picker.getImage(source: ImageSource.camera);
  setState(() {
  _image=File(pickedImage!.path);
});
 }    

and getting this image by :

var pic =await http.MultipartFile.fromPath("image", _image.path);

and dont know what is the issue ?

CodePudding user response:

Instead of using late File _image; you can try using:

File? _image;

CodePudding user response:

ImagePicker can return a null value if you don't pick any image from the image picker sheet. Therefore, you need to handle null values.

This is what you can do.

File? _image;

final picker=ImagePicker();

Future choiceImage() async{
 var pickedImage= await picker.getImage(source: ImageSource.gallery);
   if(pickedImage!=null)
     {
       setState(() 
          {
           _image=File(pickedImage!.path);
         });
      }
    }
  • Related