skip to Main Content

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


  1. Since "data" is a list, you should firstly specify the index of the value that you want to access.

    result["data"][0]
    

    another problem is since in the mentioned json example the sid value is inside another Map entry you should mention the parrent first.

    result["data"][0]["0"]["sid"]
    result["data"][0]["1"]["sid"]
    
    
    Login or Signup to reply.
  2. 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.

    sid : result["data"][0]["0"]["sid"]
    

    make these changes:

    var usedVacationsData = UsedVacationsData(
            sid : result["data"][0]["0"]["sid"],
            period: result["data"][0]["0"]["period"],
            cnt: result["data"][0]["0"]["cnt"],
            name: result["data"][0]["name"],
            img_url: result["data"][0]["img_url"],
            use_day: result["data"][0]["use_day"],
            img_rm: result["data"][0]["img_rm"],
          );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search