Reputation: 164
i am trying to loop through events from an API and load them into _kEventSource then display them in table calendar, but my code is not working neither is it showing any errors or printing anything. This file is the utils file that comes with the package by default. i have just commented the code that was looping through the generated events and want to lad my own events from the db. How do i go about it and Why is the code not running. Thanks
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 that is not working , even the print is not working and it doesnt have an error ********************************************
Map<DateTime, List<Event>> _kEventSource = {};
Future<List<Welcome>> mike() async {
String url =''; //my url goes here
final response = await http.get(Uri.parse(url));
print(response.body);
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
// 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);
Upvotes: 0
Views: 541
Reputation: 39
You need to substring year month and day, then put the last element.date inside Event() and change it to element.title just like i have done below.
Map<DateTime, List<Event>> _kEventSource = {};
Future<List<Welcome>> mike() async {
String url ='link';
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,
)],
element.date
]
: [Event(element.title)];
});
kEvents = LinkedHashMap<DateTime, List<Event>>(
equals: isSameDay,
hashCode: getHashCode,
)..addAll(_kEventSource);
return welcomeFromMap(response.body).toList();
} else {
print("response not 200");
throw Exception('Failed to load data');
}
}
Upvotes: 1