You need to enable JavaScript to run this app. Flutter Dart 如何提取Map列表中指定key的所有值并转为列表
优惠活动
大模型
产品
解决方案
定价
更多

Flutter Dart 如何提取Map列表中指定key的所有值并转为列表

Dart 提取Map列表中id字段的实现方法

你可以通过Iterable.map遍历列表后提取对应字段,最终转为List即可实现需求:

void main() {
  // 源数据
  final List<Map<String, dynamic>> sourceList = [
    {'id': 1, 'name':'flutter', 'title':'dart'},
    {'id': 35, 'name':'flutter', 'title':'dart'},
    {'id': 93, 'name':'flutter', 'title':'dart'},
    {'id': 82, 'name':'flutter', 'title':'dart'},
    {'id': 28, 'name':'flutter', 'title':'dart'},
  ];

  // 提取id生成新列表
  final idList = sourceList.map((item) => item['id'] as int).toList();

  print(idList); // 输出:[1, 35, 93, 82, 28]
}

如果需要兼容部分Map不存在id字段的场景,可以增加过滤逻辑避免异常:

final idList = sourceList
    .where((item) => item.containsKey('id') && item['id'] is int)
    .map((item) => item['id'] as int)
    .toList();

内容的提问来源于stack exchange,提问作者user16304169

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.12 20:15:03