Dart - 转换通过 HTTP REST 获取的对象的映射-API

Dart - Convert Map of objects fetched via HTTP REST-API

对于我的日历,我从后端获得以下数据 JSON (*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": "test@gmail.com",
            },
            "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": "test@gmail.com",
            },
            "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": "test@gmail.com",
            },
            "eventType": "normal"
        }
    ],
}

我的活动 Class 看起来像:

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

现在我的日历小部件需要与后端 (Map<DateTime, <List<Event>>) 中相同的结构,但我不知道如何做。如果我得到一个对象列表,我知道如何将 json 数据转换成一个对象,但是我怎样才能将日期存储为结果映射的键?

我现在的代码:

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;
    }
}

你需要做这样的事情:

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
}