Flutter 如何使用Firestore数据库快照填充表日历

Flutter 如何使用Firestore数据库快照填充表日历,flutter,Flutter,我正在尝试填充要显示在TableCalendar上的事件地图(使用table calendar软件包) 要填写这些活动,我需要一张地图 因此,在开始时,我创建了以下内容: Map<DateTime, List<Event>> _events; 从现在起,日历就可以正常工作了。 问题是当我尝试从firestore快照添加数据时。我完全不知道如何将数据从快照添加到事件映射。你会看到我将添加一条评论,如。。我在这里迷路了 Widget build(BuildContext c

我正在尝试填充要显示在TableCalendar上的事件地图(使用table calendar软件包)

要填写这些活动,我需要一张地图

因此,在开始时,我创建了以下内容:

Map<DateTime, List<Event>> _events;
从现在起,日历就可以正常工作了。 问题是当我尝试从firestore快照添加数据时。我完全不知道如何将数据从快照添加到事件映射。你会看到我将添加一条评论,如。。我在这里迷路了

Widget build(BuildContext context) {
    return Scaffold(
      appBar: popAppBar(context, 'Horaire'),
      body: StreamBuilder(
          stream: Firestore.instance
              .collection('nurseries')
              .document(widget._favoriteNurseryId)
              .collection('events')
              .snapshots(),
          builder: (context, eventsSnapshot) {
            if (eventsSnapshot.connectionState == ConnectionState.waiting) {
              return CircularProgressIndicator();
            }
            if (eventsSnapshot.hasError) {
              return Center(
                  child: Text('Il y a une erreur dans votre calendrier'));
            }
            for (var i = 0; i < eventsSnapshot.data.lenght; i++) {
              _events.addEntries( //IM LOST HERE// );
            }
            
            return TableCalendar(
              locale: 'fr_FR',
              holidays: _holidays,
              events: _events,
              calendarController: _calendarController,
              initialCalendarFormat: CalendarFormat.month,
小部件构建(构建上下文){
返回脚手架(
appBar:popAppBar(上下文“Horaire”),
正文:StreamBuilder(
流:Firestore.instance
.收藏(“托儿所”)
.document(widget.\u favoriteNurseryId)
.collection(“事件”)
.snapshots(),
生成器:(上下文,事件快照){
if(eventsSnapshot.connectionState==connectionState.waiting){
返回循环ProgressIndicator();
}
if(eventsSnapshot.hasError){
返回中心(
子项:文本('Il y a une erreur dans votre calendrier');
}
对于(var i=0;i
以下是我在项目中是如何做到这一点的

if(snapshot.hasData){
      //parse json array here and input the list of events for the months
      List<CalendarItemData> snapData = snapshot.data;
      calendarList = snapData;
      _events = convertToMap(snapData);
      print(_events.toString());
      _selectedEvents = _events[_selectedDay] ?? [];
      return buildBody();
    }else if(snapshot.hasError){
      return Padding(
        padding: const EdgeInsets.all(10),
        child:Text(
          'There are no events in the Calendar for the selected category.',
          style:TextStyle(
              fontFamily:'Raleway',
              fontWeight:FontWeight.bold,
              fontSize:18),
          textAlign:TextAlign.center,
        ),
      );
}

//method to change calendar item to Map<DateTime,List>
Map<DateTime,List> convertToMap(List<CalendarItemData> item){
Map<DateTime, List> result;

for(int i = 0; i < item.length; i++){
  CalendarItemData data = item[i];
  //get the date and convert it to a DateTime variable
  DateTime currentDate = dateFormat.parse(data.eventDate);
  List eventNames = [];
  //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.
    CalendarItemData temp = item[j];
    //establish that the temp date is equal to the current date
    if(data.eventDate == temp.eventDate) {
      //add the event name to the event List.
      eventNames.add(temp.eventName);
    } //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: eventNames
    };
  }else {
    result[currentDate] = eventNames;
  }
}

print(result);
return result;
}
if(snapshot.hasData){
//在这里解析json数组并输入月份的事件列表
List snapData=snapshot.data;
calendarList=snapData;
_事件=转换映射(snapData);
打印(_events.toString());
_selectedEvents=_事件[_selectedDay]??[];
返回buildBody();
}else if(snapshot.hasrerror){
返回填充(
填充:常量边集。全部(10),
子:文本(
“日历中没有所选类别的事件。”,
样式:TextStyle(
fontFamily:‘Raleway’,
fontWeight:fontWeight.bold,
尺寸:18),,
textAlign:textAlign.center,
),
);
}
//方法将日历项更改为映射
映射转换映射(列表项){
地图结果;
对于(int i=0;i

希望这有助于解决您的问题。

谢谢,我将在今晚或明天继续我的项目时尝试。CalendarItemData在哪里定义?
if(snapshot.hasData){
      //parse json array here and input the list of events for the months
      List<CalendarItemData> snapData = snapshot.data;
      calendarList = snapData;
      _events = convertToMap(snapData);
      print(_events.toString());
      _selectedEvents = _events[_selectedDay] ?? [];
      return buildBody();
    }else if(snapshot.hasError){
      return Padding(
        padding: const EdgeInsets.all(10),
        child:Text(
          'There are no events in the Calendar for the selected category.',
          style:TextStyle(
              fontFamily:'Raleway',
              fontWeight:FontWeight.bold,
              fontSize:18),
          textAlign:TextAlign.center,
        ),
      );
}

//method to change calendar item to Map<DateTime,List>
Map<DateTime,List> convertToMap(List<CalendarItemData> item){
Map<DateTime, List> result;

for(int i = 0; i < item.length; i++){
  CalendarItemData data = item[i];
  //get the date and convert it to a DateTime variable
  DateTime currentDate = dateFormat.parse(data.eventDate);
  List eventNames = [];
  //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.
    CalendarItemData temp = item[j];
    //establish that the temp date is equal to the current date
    if(data.eventDate == temp.eventDate) {
      //add the event name to the event List.
      eventNames.add(temp.eventName);
    } //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: eventNames
    };
  }else {
    result[currentDate] = eventNames;
  }
}

print(result);
return result;
}