{
"success": true,
"message": "",
"code": 404,
"data": [
{
"store": "store1",
"store_id": 1253,
"store_type": "B",
"created_at": 1586259248
},{
"store": "store2",
"store_id": 1253,
"store_type": "A",
"created_at": 1586259248
}
]
}
I need to access the Store key using ForEach or Map and Need to Store in Seprate list to display in listview builder how can be access that
CodePudding user response:
So let's say you have this json response.
final String json = '''{
"success": true,
"message": "",
"code": 404,
"data": [
{
"store": "store1",
"store_id": 1253,
"store_type": "B",
"created_at": 1586259248
},{
"store": "store2",
"store_id": 1253,
"store_type": "A",
"created_at": 1586259248
}
]
}''';
First you need to decode it using jsonDecode, which returns a Map<String, dynamic>
.
final Map<String, dynamic> decoded = jsonDecode(json);
Now to access your data you can just do decoded['data']
. I'm using List.from() to convert the data
to a List<Map<String, dynamic>>
.
final List<Map<String, dynamic>> data = List<Map<String, dynamic>>.from(decoded['data']);
Now if you want to get all your store
key values as a List.
final List<String> stores = data.map<String>((e)=> e['store']).toList();
This will give you the output
[store1, store2]
CodePudding user response:
Follow this tutorial: https://docs.flutter.dev/development/data-and-backend/json
You can access using Map<String, dynamic> user = jsonDecode(jsonString);
CodePudding user response:
You can use json_serializable
Or if you want to access value using dynamic then you can like the following code bloc
dynamic json=jsonDecode("you source string");
List<dynamic> list=json["data"];
dynamic item=list[0];
String store=item["store"];