How can I overwrite an assets image in Flutter having a source image?

Viewed 1117

I'm fairly new to Dart and Flutter, and I'm having trouble to overwrite an existing assets image from a source image.

My attempt:

try {
 File localFile = File('assets/images/myImage.png');
 localFile.writeAsBytesSync(originFile.readAsBytesSync());
catch (e) {
 log(e.toString());
}

I get:

[log] FileSystemException: Cannot open file, path = 'assets/images/myImage.png' (OS Error: No such file or directory, errno = 2)

I did define the assets folder in pubspec.yaml:

 assets:
    - assets/images/

Ok, so I've read somewhere that the asset file can be accessed like this:

import 'package:flutter/services.dart' show rootBundle;
final byteData = await rootBundle.load('assets/images/myImage.png');

But I don't know how to convert byteData to a File object that represents the actual file.

I think I'm missing something very basic here. Or maybe is there is a proper way to do this that has nothing to do with this approach?

Please help.

Thanks in advance!

1 Answers
Related