I have a list of dates and I want to remove the specific date range don't know how to remove it. Basically I want to remove all the time after endTime

  var endTime;
  var startTime;
  var today = now.day;
  var startOpeningHr = 06;
  var endOpeningHr = 18;
  endTime = getDateandTiming(today, endOpeningHr);
  startTime = getDateandTiming(today, startOpeningHr);

  var dates = [2021-07-24 14:30:00.000, 2021-07-24 15:00:00.000, 2021-07-24 
             15:30:00.000, 2021-07-24 16:00:00.000, 2021-07-24 16:30:00.000, 2021-07- 
             24 17:00:00.000, 2021-07-24 17:30:00.000, 2021-07-24 18:00:00.000, 2021- 
             07-24 18:30:00.000, 2021-07-24 19:00:00.000, 2021-07-24 19:30:00.000, 
             2021-07-24 20:00:00.000, 2021-07-24 20:30:00.000, 2021-07-24 
             21:00:00.000, 2021-07-24 21:30:00.000, 2021-07-24 22:00:00.000, 2021-07- 
             24 22:30:00.000, 2021-07-24 23:00:00.000, 2021-07-24 23:30:00.000, 2021- 
             07-25 00:00:00.000, 2021-07-25 00:30:00.000, 2021-07-25 01:00:00.000, 
             2021-07-25 01:30:00.000, 2021-07-25 02:00:00.000, 2021-07-25 
             02:30:00.000, 2021-07-25 03:00:00.000]

getDateandTiming(dateType, startOpening) {
    var checkDate =
        DateTime(now.year, now.month, dateType, startOpening, 0, 0);
    return checkDate;
}

I have used the removewhere but it not working as expected

 dates.removeWhere((element){
     if (endTime.isAfter(element)) {
          return null;
     }
     return element;
 });


Solution 1: Niraj

I figure out the solution, Suppose you need to remove after endTime just use removeWhere in loop in this way

 dates.removeWhere((element) {
     if (endTime.isAfter(element)) {
           return false;
     }
     return true;
  });