skip to Main Content

To begin with, i have an event Map like this:

final Map<DateTime, List<CleanCalendarEvent>>? events;

It mapped all my event based on event Date. I want to select some events in certain range of date. I know how to select all the events in one selected date, and put it in a List.

_selectedEventsList = widget.events?[DateTime(_selectedDate.year, _selectedDate.month, _selectedDate.day)] ??[];

I want to select the all events in a week. How to do this with just Map? Can i just specify it in []?

2

Answers


  1. You have to use from package:collection method firstWhereOrNull and provide a predicate function to search required dates.

    For doing this you have take date with recent Sunday and next Sunday:

    import 'package:collection/collection.dart'; // You have to add this manually, for some reason it cannot be added automatically
    
    // somewhere...
    final recentSunday = DateTime(_selectedDate.year, _selectedDate.month, _selectedDate.day - _selectedDate.weekday % 7);
    final nextSunday = DateTime(recentSunday.year, recentSunday.month, recentSunday.day + 7);
    
    _selectedEventsList = widget.events?.firstWhereOrNull((date) => date >= recentSunday && date <= nextSunday)??[];
    
    Login or Signup to reply.
  2. you can get all events within a week range with the following, with the following method:

    List eventsWithWekkRange = [];
      for(int index = 0; index< events.entries.length; index +=1) {
        DateTime currentDate = events.keys.toList()[index];
        if(currentDate.isAfter(dateOfFirstDayOfWeek) && currentDate.isBefore(dateOfFirstDayOfWeek.add(Duration(days: 7)))) {
          eventsWithWekkRange.addAll(events.values.toList()[index]);
        }}
    
     return eventsWithWekkRange;
       }
      
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search