I have an app, a booking app, where a group of student will book their mentors for lectures. Bookings will be saved on my firestore database, with the collection "event". I want to know how to use those data to be as a source of my sfCalendar:
Here is my code of the sfcalendar
SfCalendar(
view: CalendarView.month,
dataSource: EventDataSource(_getDataSource()),
// monthViewSettings: const MonthViewSettings(showAgenda: true),
monthViewSettings: const MonthViewSettings(
appointmentDisplayMode: MonthAppointmentDisplayMode.appointment,
showAgenda: true),
)
here is my code of the EventDatasource
class EventDataSource extends CalendarDataSource {
EventDataSource(List<Meeting> source) {
appointments = source;
}
@override
DateTime getStartTime(int index) {
return appointments![index].from;
}
@override
DateTime getEndTime(int index) {
return appointments![index].to;
}
@override
String getSubject(int index) {
return appointments![index].eventName;
}
@override
Color getColor(int index) {
return appointments![index].background;
}
@override
bool isAllDay(int index) {
return appointments![index].isAllDay;
}
}
This is the meeting class
class Meeting {
Meeting(this.eventName, this.from, this.to, this.background, this.isAllDay);
String eventName;
DateTime from;
DateTime to;
Color background;
bool isAllDay;
}
this is the partial _getDataSource() code
List<Meeting> _getDataSource() {
final List<Meeting> meetings = <Meeting>[];
final DateTime today = DateTime.now();
final DateTime startTime =
DateTime(today.year, today.month, today.day, 9, 0, 0);
final DateTime endTime = startTime.add(const Duration(hours: 2));
meetings.add(Meeting(
'Pitching', startTime, endTime, const Color(0xFF0F8644), false));
meetings.add(Meeting('Meeting', startTime, endTime,
const Color.fromARGB(255, 13, 58, 156), false));
return meetings;
}
I want to know how to get the data from database through a query, and assigning it inside the Meeting()
class. Anyone please help.
Check the firebase docs how to retrieve data from firestore in flutter
Basic example:
Future<List<Meeting>> _getDataSource() async {
// db should be intialized instance of firestore
final snapshot = await db.collection("event").withConverter(fromFirestore: Meeting.fromFirestore, toFirestore: Meeting.toFirestore).get();
return snapshot.docs;
}