skip to Main Content

I am confusing some Map to List conversion in dart.
For example;

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": ""
            }
        }
    },

I want to get a list include only zone details data, how to get all zone details in to a List,
I want to the output be like,

 List zoneDatas = [
     "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": ""
                    }
    ]


I am stucking this map to List conversion, Please add your valuable replays,hopefully

2

Answers


  1. Your datas declaration is not correct, I think you should add some { like this:

    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": ""
                }
            }
        }
    };
    

    Then you can access zone_deatils like this:

    Map zone_details = datas["RJGas"]["zone_deatils"];
    

    The output format that you provided is not correct, since it is a map. If you still want a list, then you can try this:

    List zone_details_as_list = datas["RJGas"]["zone_deatils"].values;
    
    Login or Signup to reply.
  2. You can try

    
    List zoneDatas = [];
    
    datas["RJGas"]["zone_deatils"].forEach((key, value) {
      zoneDatas.add({key: value});
    });
    
    print(zoneDatas);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search