I have events coming from my database which I want to display on the table calendar in flutter, how do I go about it? Here is what I have tried doing. This is in the utils.dart
file. The events are passed to the calendar.dart
file where they are loaded to the calendar via _getEventsForDay
. My challenge is in passing the data to the calendar and reading the calendar according to date.
import 'dart:collection';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:saajnairobi/announcementapi.dart';
import 'package:table_calendar/table_calendar.dart';
import 'advertannouncements.dart';
import 'package:http/http.dart' as http;
import 'announcementsapi.dart';
/// Example event class.
class Event {
final String title;
const Event(this.title);
@override
String toString() => title;
}
// LinkedHashMap<DateTime, List<AppEvent>>? _groupedEvents;
class AppEvent {
late String id;
late String userId;
late String title;
late String description;
late DateTime startDate;
}
//this is the code am trying to use, and its not running nor giving an error
Map<DateTime, List<Event>> _kEventSource = {};
Future<List<Welcome>> mike() async {
String url ='';
final response = await http.get(Uri.parse(url));
print(response.body); //this print is working but not passing data
if(response.statusCode == 200){
print(response.body);
var jsonData = welcomeFromMap(response.body).toList();
jsonData.forEach((element) {
_kEventSource[DateTime(
element.date.year,
element.date.month,
element.date.day,
)] = _kEventSource[DateTime(
element.date.year,
element.date.month,
element.date.day,
)] !=
null
? [
...?_kEventSource[DateTime(
element.date.year,
element.date.month,
element.date.day,
)],
element.date
]
: [element.date];
});
return welcomeFromMap(response.body).toList();
} else {
throw Exception('Failed to load data');
}
}
/// Example events.
/// Using a [LinkedHashMap] is highly recommended if you decide to use a map.
final kEvents = LinkedHashMap<DateTime, List<Event>>(
equals: isSameDay,
hashCode: getHashCode,
// actual code
)..addAll(_kEventSource);
// the actual code that comes with the package, this code generates events which will
//be displayed on the calender, i have commented it so that i can use my own code the
//above code
// final _kEventSource = Map.fromIterable(List.generate(365, (index) => index),
// key: (item) => DateTime.utc(kFirstDay.year, kFirstDay.month, item+1),
// value: (item) => List.generate(
// 1, (index) => Event('Muharram')))
// ..addAll({
// kToday: [
// Event('Event 1'),
// Event('Muharram 4th'),
// ],
// });
int getHashCode(DateTime key) {
return key.day * 1000000 + key.month * 10000 + key.year;
}
/// Returns a list of [DateTime] objects from [first] to [last], inclusive.
List<DateTime> daysInRange(DateTime first, DateTime last) {
final dayCount = last.difference(first).inDays + 1;
// print(dayCount);
return List.generate(
dayCount,
(index) => DateTime.utc(first.year, first.month, first.day + index),
);
}
final kToday = DateTime.now();
final kFirstDay = DateTime(2022);
final kLastDay = DateTime(2062);
i figured out what the issue was, i just had to substring the date so that it can be passed to the calendar event
Map<DateTime, List<Event>> _kEventSource = {};
Future<List<Welcome>> mike() async {
String url ='';
final response = await http.get(Uri.parse(url));
// print(response.body);
if(response.statusCode == 200){
// print("response is 200");
List jsonData = welcomeFromMap(response.body).toList();
// print(jsonData);
jsonData.forEach((element) {
// print(element.date.substring(8,10));
int year = int.parse(element.date.substring(0,4));
int month = int.parse(element.date.substring(5,7));
int day = int.parse(element.date.substring(8,10));
_kEventSource[DateTime(
year,
month,
day,
)] = _kEventSource[DateTime(
year,
month,
day,
)] != null
? [
...?_kEventSource[DateTime(
year,
month,
day,
)],
Event(element.title)
]
: [Event(element.title)];
});