【问题标题】: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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-23
    • 2020-11-27
    • 1970-01-01
    • 1970-01-01
    • 2022-12-07
    • 1970-01-01
    • 2020-08-12
    • 2020-09-22
    相关资源
    最近更新 更多