Reputation: 800
As stated, I am trying to use the flutter table_calendar: 2.3.0 and I'm not sure how to add the json data to the Map<DateTime,List> to display dates.
I'm using the start date as the datetime and the event "list" as the data that I'm utilizing.
Here is my json data:
"Events": [
{
"id": 1,
"event_name": "Cake tasting",
"event_photo": "https://dispensaries.s3.amazonaws.com/event_photo/Southern_Cali_Kush_3.jpg",
"vendor_name": {
"id": 1,
"vendor": "Tastey Cakes"
},
"refund_available": false,
"website": "www.foodcakes.com",
"share_count": 0,
"check_in_count": 0,
"street_address": "123 Fake Street",
"city": "Brooklynn",
"state": "NY",
"zipcode": "12312",
"event_tagline": "Taste my cakes",
"details": "Cake tasting",
"start_date": "2020-11-03",
"start_time": "23:33:00",
"end_time": "23:33:00",
"attendees": []
}
]
Here is the map I'm using to add the data to the _event variable that holds the new date data:
Map<DateTime, List> _events = {};
void _getEventDates(data) { Map<String, dynamic> newMap = {};
for (int i = 0; i < data.length; i++) {
newMap = {
data.startingTime, [data.name]
} as Map<String, dynamic>;
};
}
Could someone assist getting the json data into the format needed to display on the calendar?
Upvotes: 0
Views: 1558
Reputation: 3111
This convertJsonToDateMap
function will do this for you:
Map<DateTime, List> convertJsonToDateMap(String jsonSource) {
var json = jsonDecode(jsonSource);
var jsonEvents = json['Events'];
Map<DateTime, List<String>> events = {};
for(var event in jsonEvents){
var date = parseDate(event['start_date']);
events.putIfAbsent(date, () => <String>[]);
events[date].add(event['event_name']);
}
return events;
}
DateTime parseDate(String date) {
var parts = date.split('-').map(int.tryParse).toList();
return DateTime(parts[0], parts[1], parts[2]);
}
Github gist: https://gist.github.com/ali2236/3cddbbdafbc1632ddd7cfc23a1758af5
Upvotes: 1