I am a student currently studying Flutter and I want to receive this data,
so I am trying to keep trying, but I do not know how to receive it.
I’m still trying, but I’m not getting any progress. Please teach me.
Future<UsedVacationsData> fetchUsedVacationsData(
Map<String, dynamic> params) async {
Dio dio = await authDio();
final response =
await dio.get(API.usevacation_list, queryParameters: params);
var result = json.decode(response.data);
try {
var usedVacationsData = UsedVacationsData(
sid : result["data"]["sid"],
period: result["data"]["period"],
cnt: result["data"]["cnt"],
name: result["data"]["name"],
img_url: result["data"]["img_url"],
use_day: result["data"]["use_day"],
img_rm: result["data"]["img_rm"],
);
return usedVacationsData;
} catch (e) {
print(e);
}
throw new Exception('error getting vacations');
}
method(repository)
object
success HTTP/1.1 200 OK
{
"result": "SUCCESS",
"message": "",
"data": [
{
"0": {
"sid": "249",
"period": "2023.04.03",
"cnt": 0.5
},
"1": {
"sid": "249",
"period": "2023.04.05",
"cnt": 0.5
},
"name": "json",
"img_url": "",
"img_nm": "",
"use_day": 1
}
]
}
2
Answers
Since
"data"
is a list, you should firstly specify the index of the value that you want to access.another problem is since in the mentioned json example the
sid
value is inside anotherMap
entry you should mention the parrent first.Since "data" is a list of objects and contains only one object. you should specify the index number to access the object from the data list.
make these changes: