如何在Flutter中覆盖具有源图像的资产图像? [英] How can I overwrite an assets image in Flutter having a source image?

查看:97
本文介绍了如何在Flutter中覆盖具有源图像的资产图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Dart和Flutter还是很陌生,并且无法覆盖源图像中的现有资产图像.

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

我的尝试:

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

我得到:

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

我确实在 pubspec.yaml 中定义了资源文件夹:

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');

但是我不知道如何将 byteData 转换为代表实际文件的 File 对象.

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?

请帮助.

提前谢谢!

推荐答案

如果要在用户设备上写入文件,则应在此处查看:

If you want to write a file on a user device you should look here: 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.

在构建期间,Flutter将资产放入一个特殊的档案中,该档案称为应用在运行时从中读取的资产包.根据混乱的网站

希望这会有所帮助!

这篇关于如何在Flutter中覆盖具有源图像的资产图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆