【发布时间】:2021-01-08 02:36:09
【问题描述】:
我正在使用 Flutter、带有表格日历的 Firestore。
这是我的代码,目前正在运行,但有一个小故障
//Initialize Events
List<Event> eventList = List<Event>();
//Get events and add them to the list.
eventsSnapshot.data.documents.forEach((doc) {
Timestamp _eventDateStart = doc.data['eventDateStart'];
Timestamp _eventDateFinish = doc.data['eventDateFinish'];
Event _thisEvent = Event('test',
doc.data['eventName'],
doc.data['eventPrice'],
doc.data['eventDescription'],
_eventDateStart.toDate(),
_eventDateFinish.toDate());
print('Event added : ${_thisEvent.eventName.toString()}');
eventList.add(_thisEvent);
});
_events = convertToMap(eventList);
这是我的 converToMap
class Event {
final String id;
final String eventName;
final double eventPrice;
final String eventDescription;
final DateTime eventDateStart;
final DateTime eventDateFinish;
Event(this.id, this.eventName, this.eventPrice,this.eventDescription, this.eventDateStart, this.eventDateFinish);
}
//method to change calendar item to Map<DateTime,List>
Map<DateTime, List<Event>> convertToMap(List<Event> item) {
Map<DateTime, List<Event>> result;
for (int i = 0; i < item.length; i++) {
Event data = item[i];
//get the date and convert it to a DateTime variable
DateTime currentDate = data.eventDateStart;
List<Event> events = [];
//add the event name to the the eventNames list for the current date.
//search for another event with the same date and populate the eventNames List.
for (int j = 0; j < item.length; j++) {
//create temp calendarItemData object.
Event temp = item[j];
//establish that the temp date is equal to the current date
if (data.eventDateStart == temp.eventDateStart) {
//add the event name to the event List.
events.add(temp);
} //else continue
}
//add the date and the event to the map if the date is not contained in the map
if (result == null) {
result = {currentDate: events};
} else {
result[currentDate] = events;
}
}
print(result);
return result;
}
打印出来的效果就是这个。
I/flutter (1655):添加事件:deuxio I/flutter(1655):添加事件:PremierVraiTest I/flutter(1655):添加事件:测试 我/颤振(1655):{2020-09-17 13:00:00.000:[“事件”实例],2020-09-17 12:00:00.000:[“事件”实例],2020-09- 18 12:00:00.000:[“事件”实例]}
现在的问题: 当我检查我的日历时,我看到 17 的 1 个事件和 18 的 1 个事件。 17 事件是具有 13:00 的事件。我没有看到第二个事件。
【问题讨论】: