arbiter
arbiter

Reputation: 451

Display Default Image if wasn't picked by Image Picker in Flutter

I have an image picker function, that picks image from gallery and then assigns that to _image variable which is String. It converts it to base64 because that is what was necessary. I want to know how would I go about getting the default image from assets as the _image if no image was picked (picked image is null). Here's the code and things I've tried commented out, it is under else in code:

  Future _getImage() async {
PickedFile pickedFile = await picker.getImage(source: ImageSource.gallery);
setState(() {
  if (pickedFile != null) {
    final file = File(pickedFile.path);
    _image = Utility.base64String(file.readAsBytesSync());
  } else {
    //if image wasn't picked, get the default one from assets
    print('No image selected.');
    // final file = File(AssetImage('assets/defaultfood.jpg').toString());
    // _image = Utility.base64String(file.readAsBytesSync());
    //final file = File('assets/defaultfood.jpg');
    //_image = Utility.base64String(file.readAsBytesSync());
    
  }
});

}

Upvotes: 0

Views: 1330

Answers (1)

milan pithadia
milan pithadia

Reputation: 870

add image placeholder like this ternary condition

child: pickedFile == null ? Image.asset("assets/images/man_user.png",height: 100, width: 100): Image.file(pickedFile, height: 100, width: 100),

Upvotes: 2

Related Questions