0

Good evening everybody!

I was developing scheduler app. It integrates with backend (NodeJS + PSQL), the problem is when I get all schedules from Database I got it as:

[
{"schedule_id":6229,"group_id":2,"subject_id":37,"start_time":"2020-07-27 08:50","end_time":"2020-07-29 09:30","room":"24","subject_color":"#F44336","subject_title":"Rus Tili 8","teacher_id":21,"teacher_name":"Moxizar","teacher_lastname":"Baxtiyarovna","teacher_email":"brownn0910@gmail.com","teacher_password":"$2a$10$Dt8EsXu6lBD5yIK9FMYhYeZKw8LOJlaGP0RUSW4LzhtlxHlnXqXwO","teacher_phone":"+000000000","teacher_address":"Navoi 12"},
{"schedule_id":6228,"group_id":2,"subject_id":37,"start_time":"2020-07-20 08:50","end_time":"2020-07-22 09:30","room":"24","subject_color":"#F44336","subject_title":"Rus Tili 8","teacher_id":21,"teacher_name":"Moxizar","teacher_lastname":"Baxtiyarovna","teacher_email":"brownn0910@gmail.com","teacher_password":"$2a$10$Dt8EsXu6lBD5yIK9FMYhYeZKw8LOJlaGP0RUSW4LzhtlxHlnXqXwO","teacher_phone":"+000000000","teacher_address":"Navoi 12"},
{"schedule_id":6227,"group_id":2,"subject_id":37,"start_time":"2020-07-13 08:50","end_time":"2020-07-15 09:30","room":"24","subject_color":"#F44336","

Here is the thing that flutter calendar requires it to be like Map<DateTime, List<Event>>, but unfortunately in my case there is similarity in my start_time as you can mention.

So, are there any ways , to check if similar DateTime comes to create map out of this given DateTime and find all events in that DateTime and make List<Event> out of it.

I mean , how can I make like this out of my response:

Map<DateTime, List<String>> = {
 2020-09-09 : ["Event A", "Event B"],
 2020-09-10 : ["Event A", "Event B"],
}

I have tried this :

widget.schedule.forEach((element) {
      String date = element['start_time'].split(' ')[0];
      int y = int.parse(date.split('-')[0]);
      int m = int.parse(date.split('-')[1]);
      int d = int.parse(date.split('-')[2]);

      var ev = {
          "title": element['subject_title'],
          'room': element['room'],
          'start_time': element['start_time'].split(' ')[1],
          'end_time': element['end_time'].split(' ')[1],
          "isDone": true
        };

      evs[DateTime(y,m,d)] = [
        ev
      ];
      print("${DateTime(y,m,d)} : " + ev['title']);
    });

Which outputs:

I/flutter (11927): 2020-07-27 00:00:00.000 : Rus Tili 8
I/flutter (11927): 2020-07-20 00:00:00.000 : Rus Tili 8
I/flutter (11927): 2020-07-13 00:00:00.000 : Rus Tili 8
I/flutter (11927): 2020-07-06 00:00:00.000 : Rus Tili 8
I/flutter (11927): 2020-07-27 00:00:00.000 : Matem 6
I/flutter (11927): 2020-07-20 00:00:00.000 : Matem 6
I/flutter (11927): 2020-07-13 00:00:00.000 : Matem 6
I/flutter (11927): 2020-07-06 00:00:00.000 : Matem 6

But what I have as a result in my calendar :


I/flutter (11927): 2020-07-27 00:00:00.000 : Matem 6
I/flutter (11927): 2020-07-20 00:00:00.000 : Matem 6
I/flutter (11927): 2020-07-13 00:00:00.000 : Matem 6
I/flutter (11927): 2020-07-06 00:00:00.000 : Matem 6

It looks like calendar overwrites similar dates rather that combinig them

1 Answers1

1

Maps do not automatically create and add to a list upon assigning something to a duplicate key, it overwrites what was there.

To do what you want to simply need to check if the key you're trying to assign already has a List in it/exists already. If the key you're about to assign already has a value attached, add to the List instead of reassigning.

Example:

widget.schedule.forEach((element) {
    String date = element['start_time'].split(' ')[0];
    int y = int.parse(date.split('-')[0]);
    int m = int.parse(date.split('-')[1]);
    int d = int.parse(date.split('-')[2]);

    var ev = {
       "title": element['subject_title'],
       'room': element['room'],
       'start_time': element['start_time'].split(' ')[1],
       'end_time': element['end_time'].split(' ')[1],
       "isDone": true
    };

    if(!evs.containsKey(DateTime(y,m,d))) {
      evs[DateTime(y,m,d)] = [
        ev
      ];
    }
    else {
      evs[DateTime(y,m,d)].add(ev);
    }
    print("${DateTime(y,m,d)} : " + ev['title']);
});
Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
  • Additionally, as a note for your future reference, the [intl package has a `DateFormat` class](https://pub.dev/documentation/intl/latest/intl/DateFormat-class.html) that makes it easier to parse dates. It might have been of some use to you. – Christopher Moore Jul 07 '20 at 18:32