简体   繁体   English

如何覆盖具有源图像的 Flutter 中的资产图像?

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

I'm fairly new to Dart and Flutter, and I'm having trouble to overwrite an existing assets image from a source image.我对 Dart 和 Flutter 还很陌生,我无法从源图像覆盖现有资产图像。

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 :我确实在pubspec.yaml中定义了 assets 文件夹:

 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.但我不知道如何将byteData转换为代表实际文件的File object。

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!提前致谢!

If you want to write a file on a user device you should look here: https://flutter.dev/docs/cookbook/persistence/reading-writing-files如果你想在用户设备上写文件,你应该看这里: https://flutter.dev/docs/cookbook/persistence/reading-writing-files

Shared preferences are a space in a phone where you app can write, so it's exactly what you want!共享偏好是手机中的一个空间,您可以在其中编写应用程序,因此这正是您想要的!

Assets are part of you app and are not meant to be modified within the app.资产是您应用程序的一部分,不应在应用程序内进行修改。

During a build, Flutter places assets into a special archive called the asset bundle that apps read from at runtime.在构建过程中,Flutter 将资产放入名为资产包的特殊存档中,应用程序在运行时从中读取。 According to the flutter website 根据 flutter 网站

Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM