【问题标题】:Which is the best way to use image as object property in Flutter?在 Flutter 中使用图像作为对象属性的最佳方法是什么?
【发布时间】:2021-10-25 22:27:55
【问题描述】:
我在我的应用 Flutter/Dart 中使用了对象模型。但是当我想在前端设置该类对象的属性时如何渲染图像。
示例专辑类
class Album{
String title;
Image mg;
String description;
Album({this. Title, this. Mg, this. Description});
}
当我想访问像 obj1.img 这样的图像时:
new Raw Image(obj1.img)
它给出了错误。
【问题讨论】:
标签:
image
flutter
datamodel
【解决方案1】:
通常,使用 URL 检索图像。将其存储为图像对象并不是一个好的设计,因为这具有以下含义:
- 图像对象在相册对象中被引用,因此它们在对象的生命周期中消耗大量内存(很可能,相册列表在应用启动时被检索并一直保存到用户关闭应用)。
- 通常情况下,资产 url 存储在数据库中,因此在获取实体并解析它们时,您需要再次通过来下载每个实体的图像。
这是一个概念:
class Album {
final String title;
final String coverUrl;
final String description;
Album({this.title, this.coverUrl, this.description});
}
Container(
child: Image.network(artist.coverUrl,),
);
如果需要缓存,请查看CachedNetworkImage。