'How to save your api response in as a list in flutter
I am calling an api whose response is a list of items and I want to store them to response class. My code is:
await get(
Uri.parse(
"https://**********/api/Payment/GetDeliveryList?statusid=$statuscode&ShipperId=$shipperid&Rowsperpage=$rows&Page=$page"),
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer $tokens",
},
).then((response) {
var data = json.decode(response.body);
print(data);
Solution 1:[1]
if response.body is a json List you can simply but it in a List<Map<String,dynamic>>
.then((res) {
final json = jsonDecode(res.body);
if (json is List<Map<String,dynamic>>) {
List<Map<String,dynamic>> myList = json;
} else {
print("json is not list");
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MindStudio |
