I build an user profile page where you may change the profile image. The default picture is in assets. The selected picture, either from gallery or camera, is stored localy. All this works fin. My code to display the profile picture looks like:
Container(
width: 140.0,
height: 140.0,
decoration: new BoxDecoration(
shape: BoxShape.circle,
image: new DecorationImage(
image: new FileImage(File(_image.path)),
//image: new ExactAssetImage(_image.path),
fit: BoxFit.cover,
),
))
The FileImage shows the local image correctly. The ExactAssetImage shows the default asset image correctly. My question: how to check if local image exists and display it or use the default assset image?
Update:
I initialize my _image like:
File _image = File('assets/images/user_profile_static.png');
Once I have chosen the new image I save it localy using following code:
void _imgFromGallery() async {
final imageFile = await ImagePicker().getImage(source: ImageSource.gallery);
if (imageFile == null) return;
Directory directory = await getApplicationDocumentsDirectory();
File tmpFile = File(imageFile.path);
tmpFile = await tmpFile.copy('${directory.path}/my_profile.jpg');
setState(() {
_image = tmpFile;
});
}
So on next visit of the page, if my_profile.jpg exists, show it , else show default image.