I am developing an application with Flutter, currently from a button I am accessing my gallery, but when I select the image it should show me the selected image in a widget:
return Scaffold(
key: scaffoldKey,
appBar: AppBar(
title: Text('Producto'),
actions: <Widget>[
IconButton(
icon: Icon( Icons.photo_size_select_actual ),
onPressed: _seleccionarFoto,
),
IconButton(
icon: Icon( Icons.camera_alt ),
onPressed: _tomarFoto,
)
],
),
body: SingleChildScrollView(
child: Container(
padding: EdgeInsets.all(15.0),
child: Form(
key: formKey,
child: Column(
children: <Widget>[
_mostrarFoto(),
_crearNombre(),
_crearPrecio(),
_crearDisponible(),
_crearBoton(),
],
),
),
),
),
);
}
The method that selects the photo
_seleccionarFoto() async {
foto = await ImagePicker.pickImage(
source: ImageSource.gallery
);
if ( foto != null ) {
// limpieza
}
setState(() {});
}
The widget that displays the photo:
Widget _mostrarFoto() {
if ( producto.fotoUrl != null ) {
return Container();
} else {
return Image(
image: AssetImage( foto?.path ?? 'assets/no-image.png'),
height: 300.0,
fit: BoxFit.cover,
);
}
}
Selecting the image returns to the widget from where the button was pressed to access the gallery, but it does not show any image and shows the following error:
"Unable to load asset: /storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20200123-WA0049.jpg"
After that the application no longer goes back, I have to restart for it to work again. I would appreciate your help, no matter how small, thank you.
This error is due to the fact that when using
AssetImage
what it does is look for the assets that you have preloaded in the application, in your case it is not, you get theFile
image from the gallery, you need to use the widgetFileImage
.https://api.flutter.dev/flutter/painting/FileImage/FileImage.html
Something like that