Articles in this section
Category / Section

How to Load the Appointment from Json to Calendar In Flutter?

2 mins read

In the Flutter Calendar, appointments can be displayed from JSON data and loaded into the calendar using loadMoreWidgetBuilder.

STEP 1: In initState(), initialize the default values.

MeetingDataSource _events = MeetingDataSource(<_Meeting>[]);
 
@override
void initState() {
  super.initState();
}

STEP 2: Define the calendar widget with the loadMoreWidgetBuilder property.

child: SfCalendar(
  initialDisplayDate: DateTime(2017, 05, 01),
  view: CalendarView.month,
  allowedViews: [
    CalendarView.day,
    CalendarView.week,
    CalendarView.workWeek,
    CalendarView.month,
    CalendarView.timelineDay,
    CalendarView.timelineWeek,
    CalendarView.timelineWorkWeek,
    CalendarView.timelineMonth,
    CalendarView.schedule,
  ],
  dataSource: _events,
  loadMoreWidgetBuilder: loadMoreWidget,
)
 
Widget loadMoreWidget(
    BuildContext context, LoadMoreCallback loadMoreAppointments) {
  return FutureBuilder<void>(
    initialData: 'loading',
    future: loadMoreAppointments(),
    builder: (context, snapShot) {
      return Container(
          alignment: Alignment.center, child: CircularProgressIndicator());
    },
  );
}

STEP 3: The handleLoadMore retrieves appointments from JSON data and loads them into the Flutter calendar.

class MeetingDataSource extends CalendarDataSource {
  MeetingDataSource(this.source);
 
  List<_Meeting> source;
 
  @override
  List<dynamic> get appointments => source;
 
  @override
  DateTime getStartTime(int index) {
    return source[index].from;
  }
 
  @override
  DateTime getEndTime(int index) {
    return source[index].to;
  }
 
  @override
  bool isAllDay(int index) {
    return source[index].isAllDay;
  }
 
  @override
  String getSubject(int index) {
    return source[index].eventName;
  }
 
  @override
  Color getColor(int index) {
    return source[index].background;
  }
 
  @override
  Future<void> handleLoadMore(DateTime startDate, DateTime endDate) async {
    await Future<void>.delayed(const Duration(seconds: 1));
    http.Response data = await http.get(Uri.parse(
        "https://js.syncfusion.com/demos/ejservices/api/Schedule/LoadData"));
    dynamic jsonData = json.decode(data.body);
 
    final List<_Meeting> appointmentData = [];
    for (dynamic data in jsonData) {
      _Meeting meetingData = _Meeting(
          data['Subject'],
          _convertDateFromString(
            data['StartTime'],
          ),
          _convertDateFromString(data['EndTime']),
          Colors.red,
          data['AllDay']);
      appointmentData.add(meetingData);
    }
 
    appointments.addAll(appointmentData);
    notifyListeners(CalendarDataSourceAction.add, appointmentData);
  }
 
  DateTime _convertDateFromString(String date) {
    return DateTime.parse(date);
  }
}

View the GitHub sample here

Flutter Calendar Json data

 

Conclusion

I hope you enjoyed learning about how to load the appointment from json to calendar with loadMoreWidgetBuilder

You can refer to our  Flutter Calender feature tour page to know about its other groundbreaking feature representations. You can also explore our Flutter Calendar documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied