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:
- Extract the
zone_deatilsMap from your original data structure. - Iterate over each key-value entry in this Map, creating a new single-key Map for each entry.
- 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 thezone_deatilsMap 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
相关产品推荐
相关产品推荐

