Thanks!
I tried following that example but am still getting the same error.
Below is the code and the custom appointment class. Please let me know what I'm doing wrong. Thanks!
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:starter_architecture_flutter_firebase/app/home/models/shift.dart';
import 'package:starter_architecture_flutter_firebase/app/top_level_providers.dart';
import 'package:syncfusion_flutter_calendar/calendar.dart';
final shiftsStreamProvider = StreamProvider.autoDispose<List<Shift>>((ref) {
final database = ref.watch(databaseProvider)!;
return database.shiftsStream();
});
class Scheduler extends ConsumerWidget {
final GlobalKey _globalKey = GlobalKey();
final ScrollController _controller = ScrollController();
final CalendarController _calendarController = CalendarController();
final List<CalendarView> _allowedViews = <CalendarView>[
CalendarView.day,
CalendarView.week,
CalendarView.workWeek,
CalendarView.month,
CalendarView.schedule
];
Scheduler({Key? key}) : super(key: key);
@override
Widget build(BuildContext context, WidgetRef ref) {
final shiftsStream = ref.watch(shiftsStreamProvider);
return shiftsStream.when(
data: (data) {
return Scaffold(
body: SfCalendar(
controller: _calendarController,
allowedViews: _allowedViews,
dataSource: _ShiftDataSource(data),
allowDragAndDrop: true,
),
);
},
loading: () {
return const CircularProgressIndicator();
},
error: (Object error, StackTrace? stackTrace) {
return Scaffold(body: Text('Error $error $stackTrace'));
},
);
}
}
/// An object to set the appointment collection data source to collection, which
/// used to map the custom appointment data to the calendar appointment, and
/// allows to add, remove or reset the appointment collection.
class _ShiftDataSource extends CalendarDataSource<Shift> {
_ShiftDataSource(this.source);
List<Shift> source;
@override
List<dynamic> get appointments => source;
@override
String getId(int index) {
return source[index].id;
}
@override
DateTime getStartTime(int index) {
return source[index].startTime;
}
@override
DateTime getEndTime(int index) {
return source[index].endTime;
}
@override
bool isAllDay(int index) {
return source[index].isAllDay;
}
@override
String getSubject(int index) {
return source[index].subject;
}
@override
Color getColor(int index) {
return source[index].color;
}
@override
Shift convertAppointmentToObject(Shift eventName, Appointment appointment) {
return Shift(
id: appointment.id.toString(),
subject: appointment.subject,
startTime: appointment.startTime,
endTime: appointment.endTime,
color: appointment.color,
isAllDay: appointment.isAllDay);
}
}
And the custom appointment type:
import 'package:flutter/material.dart';
import 'package:syncfusion_flutter_calendar/calendar.dart';
class Shift extends Appointment {
Shift({
required this.id,
required DateTime startTime,
required DateTime endTime,
required bool isAllDay,
required String subject,
required Color color,
String? startTimeZone,
String? endTimeZone,
String? recurrenceRule,
List<DateTime>? recurrenceExceptionDates,
String? notes,
String? location,
List<Object>? resourceIds,
Object? recurrenceId,
// Object? id,
}) : super(
startTime: startTime,
endTime: endTime,
isAllDay: isAllDay,
subject: subject,
color: color,
startTimeZone: startTimeZone,
endTimeZone: endTimeZone,
recurrenceRule: recurrenceRule,
recurrenceExceptionDates: recurrenceExceptionDates,
notes: notes,
location: location,
resourceIds: resourceIds,
recurrenceId: recurrenceId,
// id: id,
// );
);
final String id;
Map<String, dynamic> toMap() {
return {
'startTime': startTime,
'endTime': endTime,
'isAllDay': isAllDay,
'subject': subject,
// 'colorString': color.toString(),
// 'color': color,
'startTimeZone': startTimeZone,
'endTimeZone': endTimeZone,
'recurrenceRule': recurrenceRule,
'recurrenceExceptionDates': recurrenceExceptionDates,
'notes': notes,
'location': location,
'resourceIds': resourceIds,
'recurrenceId': recurrenceId,
'id': id,
};
}
factory Shift.fromMap(Map<dynamic, dynamic>? value, String shiftId) {
if (value == null) {
throw StateError('missing data for shiftId: $shiftId');
}
final id = value['id'] as String;
final startTime = value['startTime'].toDate() as DateTime;
final endTime = value['endTime'].toDate() as DateTime;
final isAllDay = value['isAllDay'] as bool;
final subject = value['subject'] as String;
// final colorString = value['colorString'] as String;
// final color = value['color'] as Color;
return Shift(
id: id,
startTime: startTime,
endTime: endTime,
isAllDay: isAllDay,
subject: subject,
color: Colors.lightBlueAccent,
// color: colorString,
// id: id,
// resourceIds: resourceIds.cast(),
// comment: value['comment'] as String? ?? '',
);
}
}