Unable to load asset in Flutter


I’m sharing a picture that I’ve taken previously with the camera using image_picker. It works fine on the emulator but not on my device:

onPressed: () async {
  ByteData image = await rootBundle.load(team.avatar.path);

This is the error with the path:

Unable to load asset:

The image is being shown without errors so the path is 100% correct:

  fit: BoxFit.cover,
  height: 92.0,
  width: 92.0,

Do I need to add something else maybe on the pubspec.yml?


You can’t use the rootBundle to access files on the phone. The rootBundle (as said in its docs) only works for files packaged with the application when was built (probably saved on assets, declared on pubspec, etc).

If you want to load an image from the phone, this maybe help.


This function can read a filePath and return a Uint8List (a byteArray):

Future<Uint8List> _readFileByte(String filePath) async {
    Uri myUri = Uri.parse(filePath);
    File audioFile = new File.fromUri(myUri);
    Uint8List bytes;
    await audioFile.readAsBytes().then((value) {
    bytes = Uint8List.fromList(value); 
    print('reading of bytes is completed');
  }).catchError((onError) {
      print('Exception Error while reading audio from path:' +
  return bytes;

And, to get a ByteData just:

var path = 'Some path to an image';
var byteArray = _readFileByte(path);
ByteData data = ByteData.view(byteArray.buffer);

(The answer is based on this)

Answered By – Ademir Villena Zevallos

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published