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

Dart中如何将Map的zone_deatils数据转换为List?

Dart: Convert Map's zone_deatils to Target List

To convert the zone_deatils section of your Map into the desired List format, follow these steps:

  1. Extract the zone_deatils Map from your original data structure.
  2. Iterate over each key-value entry in this Map, creating a new single-key Map for each entry.
  3. Convert the resulting iterable of Maps into a List.

Here's the complete working code:

void main() {
  Map<String, dynamic> datas = {
    "RJGas": {
      "unit_deatils": {
        "total_obs": 3,
        "previous_obs": 3,
        "difference": 0,
        "indicator": "green",
        "critical_period": ["17:00", "17:59"],
        "critical_obs": "HK"
      },
      "zone_deatils": {
        "RGT Well Pads 04": {
          "total_obs": 0,
          "previous_obs": 2,
          "difference": -2,
          "indicator": "green",
          "critical_period": [],
          "critical_obs": ""
        },
        "RGT Well pads 01": {
          "total_obs": 2,
          "previous_obs": 0,
          "difference": 2,
          "indicator": "red",
          "critical_period": ["17:00", "17:59"],
          "critical_obs": "HK"
        },
        "RGT Well": {
          "total_obs": 1,
          "previous_obs": 1,
          "difference": 0,
          "indicator": "green",
          "critical_period": [],
          "critical_obs": ""
        },
        "RGT Well 01": {
          "total_obs": 0,
          "previous_obs": 0,
          "difference": 0,
          "indicator": "green",
          "critical_period": [],
          "critical_obs": ""
        }
      }
    }
  };

  // Extract the target zone_deatils Map
  Map<String, dynamic> zoneDetails = datas["RJGas"]["zone_deatils"];

  // Convert to List of single-key Maps
  List<Map<String, dynamic>> zoneDatas = zoneDetails.entries
      .map((entry) => {entry.key: entry.value})
      .toList();

  // Output the result (matches your desired format)
  print(zoneDatas);
}

Key Details:

  • zoneDetails.entries: Gets all key-value pairs from the zone_deatils Map as an iterable.
  • .map((entry) => {entry.key: entry.value}): Transforms each entry into a standalone Map with just that key and its associated data.
  • .toList(): Converts the iterable of Maps into a List, producing exactly the structure you need.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 03:07:45