在颤振中从 DocumentSnapshot 创建列表

时间:2021-07-03 14:34:40

标签: list flutter

好的,我对 flutter/Dart 还很陌生,我学到了很多东西。但是,我需要帮助了解如何将 Firestore DocumentSnapshot 中的数据添加到列表中。

我正在尝试使用 .addAll,但我确定我用错了。

LinkedHashMap<DateTime, List<Event>> kEvents;

List<Event> _getEventsForDay(DateTime day) {
    // kEvents is a linkedHashMap
    for (int i = 0; i < eventDoc.length; i++ ) {
      DateTime eventDate = eventDoc[i].eventDate;
      DateTime eventDateUTC = eventDate.toUtc();
      if (day.year == eventDate.year && day.day == eventDate.day && day.month == eventDate.month) {
        List<Event> eventList;
        eventList.addAll(eventDoc[i]);
        return kEvents.putIfAbsent(eventDateUTC, () => eventList);
      }
    }
}

这是我得到的错误:

“事件”类型不是“可迭代”类型的子类型 另见:https://flutter.dev/docs/testing/errors

这是事件类:

class Event {
  final String eventName;
  //final TimeOfDay eventStartTime;
  final DateTime eventStartTime;
  final String eventDuration;
  final DateTime eventDate;
  final String eventDescription;
  final String agentId;
  final String agencyId;


  Event(
      {this.eventName,
      this.eventStartTime,
      this.eventDuration,
      this.eventDate,
      this.eventDescription,
      this.agentId,
      this.agencyId});

  Map<String, dynamic> toMap() {
    return {
      'eventName': eventName,
      'eventStartTime': eventStartTime,
      'eventDuration': eventDuration,
      'eventDate': eventDate,
      'eventDescription': eventDescription,
      'agentId': agentId,
      'agencyId': agencyId,
    };
  }

  // pass in a map and get an object back
  Event.fromFirestore(Map<String, dynamic> firestore)
      : eventName = firestore['eventName'],
        eventStartTime = firestore['eventStartTime'].toDate(),
        eventDuration = firestore['eventDuration'],
        eventDate = firestore['eventDate'].toDate(),
        eventDescription = firestore['eventDescription'],
        agentId = firestore['agentId'],
        agencyId = firestore['agencyId'];
}

这是 Firestore 中的事件记录示例: enter image description here

有人可以在这里帮助我使用 .addAll 方法或向我展示一种更好的方法吗?

1 个答案:

答案 0 :(得分:0)

eventList.add(Event) 和 eventList.addAll(Iterable), 而 eventDoc[i] 是一个 Event 而不是 Iterable。