Home > Blockchain >  Flutter Http response body from String to List of Object
Flutter Http response body from String to List of Object

Time:08-25

Hi I am currently trying to fetch some data from an API, for later casting it to my Object Class.

The json answer i receive is instead of a list, directly a String.

{
"00-01": {
    "date": "24-08-2022",
    "hour": "00-01",
    "is-cheap": false,
    "is-under-avg": false,
    "market": "PVPC",
    "price": 617.5,
    "units": "€/Mwh"
},
"01-02": {
    "date": "24-08-2022",
    "hour": "01-02",
    "is-cheap": false,
    "is-under-avg": false,
    "market": "PVPC",
    "price": 640.05,
    "units": "€/Mwh"
},
"02-03": {
    "date": "24-08-2022",
    "hour": "02-03",
    "is-cheap": false,
    "is-under-avg": false,
    "market": "PVPC",
    "price": 670.26,
    "units": "€/Mwh"
},
"03-04": {
    "date": "24-08-2022",
    "hour": "03-04",
    "is-cheap": false,
    "is-under-avg": false,
    "market": "PVPC",
    "price": 683.64,
    "units": "€/Mwh"
},
"04-05": {
    "date": "24-08-2022",
    "hour": "04-05",
    "is-cheap": false,
    "is-under-avg": false,
    "market": "PVPC",
    "price": 692.88,
    "units": "€/Mwh"
},
"05-06": {
    "date": "24-08-2022",
    "hour": "05-06",
    "is-cheap": false,
    "is-under-avg": false,
    "market": "PVPC",
    "price": 681.87,
    "units": "€/Mwh"
},
"06-07": {
    "date": "24-08-2022",
    "hour": "06-07",
    "is-cheap": false,
    "is-under-avg": false,
    "market": "PVPC",
    "price": 624.35,
    "units": "€/Mwh"
},
"07-08": {
    "date": "24-08-2022",
    "hour": "07-08",
    "is-cheap": false,
    "is-under-avg": false,
    "market": "PVPC",
    "price": 624.82,
    "units": "€/Mwh"
},
"08-09": {
    "date": "24-08-2022",
    "hour": "08-09",
    "is-cheap": false,
    "is-under-avg": false,
    "market": "PVPC",
    "price": 623.24,
    "units": "€/Mwh"
},
"09-10": {
    "date": "24-08-2022",
    "hour": "09-10",
    "is-cheap": false,
    "is-under-avg": true,
    "market": "PVPC",
    "price": 558.55,
    "units": "€/Mwh"
},
"10-11": {
    "date": "24-08-2022",
    "hour": "10-11",
    "is-cheap": false,
    "is-under-avg": true,
    "market": "PVPC",
    "price": 511.3,
    "units": "€/Mwh"
},
"11-12": {
    "date": "24-08-2022",
    "hour": "11-12",
    "is-cheap": false,
    "is-under-avg": true,
    "market": "PVPC",
    "price": 493.36,
    "units": "€/Mwh"
},
"12-13": {
    "date": "24-08-2022",
    "hour": "12-13",
    "is-cheap": true,
    "is-under-avg": true,
    "market": "PVPC",
    "price": 484.42,
    "units": "€/Mwh"
},
"13-14": {
    "date": "24-08-2022",
    "hour": "13-14",
    "is-cheap": true,
    "is-under-avg": true,
    "market": "PVPC",
    "price": 487.58,
    "units": "€/Mwh"
},
"14-15": {
    "date": "24-08-2022",
    "hour": "14-15",
    "is-cheap": true,
    "is-under-avg": true,
    "market": "PVPC",
    "price": 426.72,
    "units": "€/Mwh"
},
"15-16": {
    "date": "24-08-2022",
    "hour": "15-16",
    "is-cheap": true,
    "is-under-avg": true,
    "market": "PVPC",
    "price": 418.28,
    "units": "€/Mwh"
},
"16-17": {
    "date": "24-08-2022",
    "hour": "16-17",
    "is-cheap": true,
    "is-under-avg": true,
    "market": "PVPC",
    "price": 422.18,
    "units": "€/Mwh"
},
"17-18": {
    "date": "24-08-2022",
    "hour": "17-18",
    "is-cheap": true,
    "is-under-avg": true,
    "market": "PVPC",
    "price": 430.63,
    "units": "€/Mwh"
},
"18-19": {
    "date": "24-08-2022",
    "hour": "18-19",
    "is-cheap": false,
    "is-under-avg": true,
    "market": "PVPC",
    "price": 495.26,
    "units": "€/Mwh"
},
"19-20": {
    "date": "24-08-2022",
    "hour": "19-20",
    "is-cheap": false,
    "is-under-avg": false,
    "market": "PVPC",
    "price": 579.65,
    "units": "€/Mwh"
},
"20-21": {
    "date": "24-08-2022",
    "hour": "20-21",
    "is-cheap": false,
    "is-under-avg": false,
    "market": "PVPC",
    "price": 614.08,
    "units": "€/Mwh"
},
"21-22": {
    "date": "24-08-2022",
    "hour": "21-22",
    "is-cheap": false,
    "is-under-avg": false,
    "market": "PVPC",
    "price": 625.97,
    "units": "€/Mwh"
},
"22-23": {
    "date": "24-08-2022",
    "hour": "22-23",
    "is-cheap": false,
    "is-under-avg": false,
    "market": "PVPC",
    "price": 582.99,
    "units": "€/Mwh"
},
"23-24": {
    "date": "24-08-2022",
    "hour": "23-24",
    "is-cheap": false,
    "is-under-avg": false,
    "market": "PVPC",
    "price": 617.25,
    "units": "€/Mwh"
}

}

I would like to cast that result into a LightHours, where every one would be a class Hour inside.

My main ideas was to get a List<LightHours> = {Hours,...}

Also I have tried to do the following: LightPrice lightPrice = LightPrice.fromJson(jsonDecode(response.body)); but it only gets me every field as null.

But using the json to Dart converter it doesn't allow me.

How could i convert that response into both objects classes?

CodePudding user response:

Something like this maybe works:

List<LightPrice> list = (jsonDecode(response.body) as Map<String,
    Map<String, dynamic>>).values.map<LightPrice>((value) =>
    LightPrice.fromJson(value)).toList();

CodePudding user response:

//your json string
String jsonString = json.encode(data);

//convert json string to list
List<String> newData = List<String>.from(json.decode(jsonString));

this will help to convert

  • Related