Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Unable to load asset" with image picker

Tags:

flutter

dart

I'm using Image Picker package in my Flutter project
I choose image from gallery then preview it in Image.asset widget

The problem here is if image name "example_name.png" (without spaces) the image is visible on the screen, but if image name "example name.png" (with spaces) the image is invisible like this Screenshot.

Error: Unable to load asset: /storage/emulated/0/Download/images (9).jpeg

File _image;


Image.asset(
  _image != null
      ? "${_image.path}"
      : getImage("icon.png"),
  fit: BoxFit.cover,
  width: 120,
  height: 120,
);

...

Future chooseFile() async {    
  await ImagePicker.pickImage(source: ImageSource.gallery).then((image) {    
    setState(() {    
      _image = image;    
    });    
  });    
}
like image 780
Salah Rashad Avatar asked Dec 11 '22 02:12

Salah Rashad


2 Answers

You are using the wrong Image constructor. Use Image.file instead of Image.asset. Image.asset load files packaged in the application (assets section of pubspec.yaml) and ImagePicker does not have access to them.

like image 111
Spatz Avatar answered Dec 15 '22 00:12

Spatz


in the image_picker (version 0.6.7 + 22) I was able to recover the image with this condition

    if (photo == null) {
        return Image (
          image: AssetImage ('assets / no-image.png'),
          height: 300.0,
          fit: BoxFit.cover,
        );
      } else {
        return Image.file (
          Photo,
          height: 300.0,
          fit: BoxFit.cover,
        );
      }
like image 34
Jhon BN Avatar answered Dec 14 '22 23:12

Jhon BN