Home > Software engineering >  Flutter how to filter array
Flutter how to filter array

Time:01-20

Please give me the solution for this. I have json data here

{
"id": "61e7e10f5c5677",
"travel_map": [
    {
        "odometer": 0,
        "coords": {
            "altitude": 150.1,
            "heading": -1,
            "latitude": 28.5799048,
            "speed": -1,
            "longitude": 77.3191382
        }
    },
    {
        "odometer": 0,
        "coords": {
            "altitude": 149.8,
            "heading": 315.43,
            "latitude": 28.5799129,
            "speed": 0.01,
            "longitude": 77.3191291
        }
    }
]
}

I need to filter latitude and longitude from these data in array like this

[LatLng(28.5799048, 77.3191382), LatLng(28.5799129, 77.3191291)]

CodePudding user response:

Use Json to dart converter

if you get the response from api do like this

final parsedData=json.decode(response.body);
MyJsonToDartData data= new MyJsonToDartData.fromJson(parsedData);

now you can use the data as a object and access all the underlying values

CodePudding user response:

Try this:

var travelMapList = yourJson["travel_map"] as List? ?? [];
var latLangList = travelMapList.map((e) => LatLng(e["coords"]["latitude"] ?? 0, e["coords"]["longitude"] ?? 0));
print(latLangList);

This will print

(Instance of 'LatLng', Instance of 'LatLng')
  •  Tags:  
  • Related