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

在Flutter Map中如何仅访问对应的值?

如何在Flutter Map中仅访问值?

首先,把你提供的JSON结构转换成Flutter中的Map对象:

Map<String, dynamic> turfAmenities = {
  "turf_washroom": true,
  "turf_water": true,
  "turf_dressing": false,
  "turf_parking": true,
  "turf_gallery": true,
  "turf_cafeteria": true
};

这里有几种常用的方式来访问Map中的值:

  • 获取所有值的集合
    Map自带的values属性可以直接返回所有值的Iterable对象,你可以直接遍历或者转成List:

    // 获取所有值的 Iterable
    Iterable<bool> amenityValues = turfAmenities.values;
    // 转换为 List 方便后续操作
    List<bool> amenityValueList = amenityValues.toList();
    
  • 遍历Map时单独取值
    直接遍历values集合,逐个处理每个值:

    for (bool value in turfAmenities.values) {
      print(value); // 输出每个设施的状态
    }
    
  • 根据键获取单个值
    如果只需要特定键对应的值,直接用键索引Map即可,建议加上空值处理避免异常:

    // 获取turf_washroom对应的值,不存在时默认返回false
    bool hasWashroom = turfAmenities['turf_washroom'] ?? false;
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 19:40:57