Home > Back-end >  Dart - Convert Map of objects fetched via HTTP REST-API
Dart - Convert Map of objects fetched via HTTP REST-API

Time:05-30

For my Calendar i get the following data as JSON from the Backend (*JAVA-Type = Map<LocalDate, List<Event>>):

{
    "2022-05-28": [
        {
            "id": 2,
            "title": "Multi day Event",
            "fromDate": "2022-05-27T12:22:03.873569",
            "toDate": "2022-05-28T11:22:03.873569",
            "room": {
                "id": 1,
                "name": "TestRoom",
            },
            "user": {
                "id": 1,
                "name": "Andi",
                "city": "",
                "email": "[email protected]",
            },
            "eventType": "sozial"
        }
    ],
    "2022-05-27": [
        {
            "id": 2,
            "title": "Multi day Event",
            "fromDate": "2022-05-27T12:22:03.873569",
            "toDate": "2022-05-28T11:22:03.873569",
            "room": {
                "id": 1,
                "name": "TestRoom",
            },
            "user":  {
                "id": 1,
                "name": "Andi",
                "city": "",
                "email": "[email protected]",
            },
            "eventType": "sozial"
        },
        {
            "id": 1,
            "title": "Testevent",
            "fromDate": "2022-05-27T11:21:04.573754",
            "toDate": "2022-05-27T12:21:04.573754",
            "room": {
                "id": 1,
                "name": "TestRoom",
            },
            "user":  {
                "id": 1,
                "name": "Andi",
                "city": "",
                "email": "[email protected]",
            },
            "eventType": "normal"
        }
    ],
}

My Event Class looks like:

Class Event {
    int id;
    String title;
    DateTime fromDate;
    DateTime toDate;
    Room room;
    User user;
    String eventType;
}

Now i need the same structure i had in the Backend (Map<DateTime, <List<Event>>) for my Calendar widget and i have no real clue on how to do it. I know how to convert json data into an object if i get a list of an object, but how can i store the date as key of the resulting map?

My code by now:

Future<Map<DateTime, List<Event>>> getEvents(DateTime _fromDate, DateTime 
_endDate) async {
    String _from = _fromDate.toString().split('.').first;
    String _end = _endDate.toString().split('.').first;

    final response = await get('${_url}calendar/events/$_from/$_end',
    headers: {HttpHeaders.authorizationHeader: 'Bearer $_bearer'});
    if (response.status.hasError) {
       return Future.error('${response.statusText}');
    } else {
       final parsed = jsonDecode(response.body);
       return parsed;
    }
}

CodePudding user response:

You need to do something like that:

var json = {...}; // <-- json obj
  
  // method to parse data to map with list Event
  dynamic fromJson(Map<String, dynamic> json){
    var map = new Map();
    json.keys.forEach((key){
      // key is the date
      map[key] = json[key].map((e) => Event.fromJson(e)).toList(); // <- need to create a method fromJson in your Event class
    });
    return map;
  }
  
  (...)

class Event {
    int id;
    String title;
    DateTime fromDate;
    DateTime toDate;
    Room room;
    User user;
    String eventType;
  
  fromJson(Map<String, dynamic> json) => Event(...); // <- parse json to Event class
}
  • Related