1 year ago

#377194

test-img

Mahmoud Saleh

How to upload an image from assets folder to server in flutter?

I need to let user pick an image from assets/images directory, I did it. now i need to upload image selected to backend guy, i used this snap of code to convert asset path to file :

  Future<File> getImageFileFromAssets(String path) async {
    final byteData = await rootBundle.load('$path');
    final file = File('${(await getTemporaryDirectory()).path}/$path');
    await file.writeAsBytes(byteData.buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
    return file;
  }

I test the file in this line

final file = File('${(await getTemporaryDirectory()).path}/$path');

and got this as file path:

'/data/user/0/com.mohra.app/cache/assets/images/svg/dream_icons/dream-icon-9.png'

but error i got in this line :

await file.writeAsBytes(byteData.buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));

the error is :

Unhandled Exception: FileSystemException: Cannot open file, path = '/data/user/0/com.mohra.app/cache/assets/images/svg/dream_icons/dream-icon-9.png' (OS Error: No such file or directory, errno = 2)

any one can help how to create file from assets path ??

flutter

image

file

upload

assets

0 Answers

Your Answer

Accepted video resources