i want to use table_calendar with firebase data, but still having issue passing events into the widget ....
Sample structure of firebase data:
{
'date' : 26 Jun
'name' : A
'date' : 26 Jun
'name' : B
'date' : 11 Feb
'name' : C
}
i want to create the Map in this way:
{
'26 Jun' : [{'date' : 26 Jun, 'name' : A} , {'date' : 26 Jun, 'name' : B}]
'11 Feb' : [{'date' : 11 Feb, 'name' : C}]
}
below code was trying to use Map.fromIterable but i'm not sure how to merge data with same key...
final _kEventSource = Map<DateTime, List<dynamic>>.fromIterable(bookings,
key: (item) => item['date'].toDate(),
value: (item) => List.generate(1, (index) => item))
================================================================
UPDATE:
var newMap = groupBy(bookings, (obj) => (obj as Map)['date'].toDate()).map((k, v) => MapEntry<DateTime, List<dynamic>>(
k,
v.map((item) {
item.remove('date');
return item;
}).toList()));
print(newMap);
final kEvents = LinkedHashMap<DateTime, List<dynamic>>(equals: isSameDay, hashCode: getHashCode)
..addAll(newMap);
return kEvents[day] ?? [];
}
i cannot execute the code above , seeing error with
The method 'toDate' was called on null.
Receiver: null
Tried calling: toDate()
CodePudding user response:
With the collection package you can group by date. Try this code:
import "package:collection/collection.dart";
void main() {
var data = [
{'date': "26 Jun", 'name': "A"},
{'date': "26 Jun", 'name': "A"},
{'date': "11 Feb", 'name': "C"},
];
var newMap = groupBy(data, (Map obj) => obj['date']).map((k, v) => MapEntry(
k,
v.map((item) {
item.remove('date');
return item;
}).toList()));
print(newMap);
}
Output:
{26 Jun: [{name: A}, {name: A}], 11 Feb: [{name: C}]}