getVisibleAppointments method Null safety

List<Appointment> getVisibleAppointments(
  1. DateTime startDate,
  2. String calendarTimeZone,
  3. [DateTime? endDate]
)

Returns the appointments in the specified date range.

startDate - required - The starting date from which to obtain the appointments.

endDate - optional - The end date till which to obtain the visible appointments.


class MyAppState extends State<MyApp> {

  CalendarController _calendarController = CalendarController();
  late _AppointmentDataSource _dataSource;

  @override
  initState() {
  _dataSource = _getCalendarDataSource();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: SfCalendar(
          view: CalendarView.month,
          controller: _calendarController,
          dataSource: _dataSource,
          onViewChanged: (ViewChangedDetails details) {
            List<DateTime> dates = details.visibleDates;
            String calendarTimeZone = '';
            List<Object> appointment = _dataSource.getVisibleAppointments(
                dates[0], calendarTimeZone,
                dates[(details.visibleDates.length) - 1]);
          },
        ),
      ),
    );
  }
}

_AppointmentDataSource _getCalendarDataSource() {
  List<Appointment> appointments = <Appointment>[];
  appointments.add(Appointment(
    startTime: DateTime(2020, 11, 27, 9),
    endTime: DateTime(2020, 11, 27, 9).add(Duration(hours: 2)),
    subject: 'Meeting',
    color: Colors.cyanAccent,
    startTimeZone: '',
    endTimeZone: '',
    recurrenceRule: 'FREQ=DAILY;INTERVAL=2;COUNT=5',
  ));
  appointments.add(Appointment(
      startTime: DateTime(2020, 11, 28, 5),
      endTime: DateTime(2020, 11, 30, 7),
      subject: 'Discussion',
      color: Colors.orangeAccent,
      startTimeZone: '',
      endTimeZone: '',
      isAllDay: true
  ));
  return _AppointmentDataSource(appointments);
}

class _AppointmentDataSource extends CalendarDataSource {
  _AppointmentDataSource(List<Appointment> source) {
    appointments = source;
  }
}

Implementation

List<Appointment> getVisibleAppointments(
    DateTime startDate, String calendarTimeZone,
    [DateTime? endDate]) {
  endDate ??= startDate;

  /// Converts the given appointment type to calendar appointment, to handle
  /// the internal operations like timezone converting.
  /// Calendar appointment is an internal class to handle the appointment
  /// rendering on view.
  List<CalendarAppointment> calendarAppointments =
      AppointmentHelper.generateCalendarAppointments(this, calendarTimeZone);

  calendarAppointments = AppointmentHelper.getVisibleAppointments(
      startDate, endDate, calendarAppointments, calendarTimeZone, false,
      canCreateNewAppointment: false);

  final List<Appointment> visibleAppointments = <Appointment>[];

  for (int i = 0; i < calendarAppointments.length; i++) {
    visibleAppointments
        .add(calendarAppointments[i].convertToCalendarAppointment());
  }

  return visibleAppointments;
}