You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter(Dart) 如何读取嵌套列表中存储的imageUrl图片资源路径?

问题解决方案

错误原因

你的原始数据结构中imageUrl字段的数组内部多余嵌套了一层大括号{},按照你存储图片路径的需求,这层大括号没有实际作用,会导致lstUsers[index]["imageUrl"][0]拿到的是集合类型(Dart中为Set),而非预期的字符串路径,传入AssetImage时触发类型不匹配报错。

修复方法

方法1:优先修正数据结构(推荐)

删除imageUrl数组内多余的大括号,改为标准字符串数组格式:

List users = [
  {
   "id": "p1",
   "name": "John Doe",
   "age": "44",
   "imageUrl": [
      "assets/images/anastasia-vityukova-unsplash-blackandwhiteimage.png",
      "assets/images/anastasia-vityukova-unsplash.png",
   ],
   "profession": "Financial Consultant"
  },
  {
   "id": "p2",
   "name": "John Doe2",
   "age": "42",
   "imageUrl": [
      "assets/images/good-faces-hiba-unsplash-blackandwhiteimage.png",
      "assets/images/good-faces-hiba-unsplash.jpg",
   ],
   "profession": "Financial Consultant"
  },
];

修正后你原来的读取代码即可正常运行:

decoration: BoxDecoration(
  image: DecorationImage(
      image: AssetImage(lstUsers[index]["imageUrl"][0]),
      fit: BoxFit.cover),
),

方法2:不修改原始数据的适配写法

如果无法改动原始数据结构,需要先将嵌套的集合转为数组再取值:

decoration: BoxDecoration(
  image: DecorationImage(
      image: AssetImage((lstUsers[index]["imageUrl"][0] as Set).toList()[0]),
      fit: BoxFit.cover),
),

内容的提问来源于stack exchange,提问作者who-aditya-nawandar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 20:54:05