从firestore、Flutter获取数据

问题描述 投票:0回答:1

我无法在我正在开发的日历中显示

List<Event>
。尽管从 Firestore 调用它,但我无法检索事件列表。我怀疑这个问题可能与我编写控制器的方式有关。

models.dart

class Event {
  final String Id;
  final String user;
  final String title;
  final String description;
  final String? type;
  final DateTime from;
  final DateTime to;
  final Color backgroundColor;

  Event({
    required this.Id,
    required this.user,
    required this.title,
    required this.description,
    this.type,
    required this.from,
    required this.to,
    this.backgroundColor = pinkColor,
  });

  Map<String, dynamic> ToMap() {
    return {
      "Id": Id,
      "user": user,
      "title": title,
      "description": description,
      "type": type,
      "from": from,
      "to": to,
    };
  }

  factory Event.fromMap(Map<String, dynamic> map, String documentId) {
    return Event(
        Id: documentId,
        user: map["user"] as String,
        title: map["title"] as String,
        description: map["description"] as String,
        type: map["TYPE"] as String,
        from: map["from"],
        to: map["to"]);
  }
}

数据库.dart

class Database {


  Future GetEvents(String user) async {
    return await FirebaseFirestore.instance
        .collection(ApiPath.Event())
        .where("email", isEqualTo: user)
        .snapshots();
  }
}

控制器.dart

class EventController {
  String currentuser = Auth().getCurrentUserEmail();


  Future<List<Event>> GetEventsController() async {
    List<Event> events = [];
    Stream<QuerySnapshot> data = await Database().GetEvents(currentuser);
    data.listen((QuerySnapshot e) {
      for (DocumentSnapshot doc in e.docs) {
        Map<String, dynamic> map = doc.data() as Map<String, dynamic>;
        Event newevent = Event.fromMap(map, map["id"]);
        events.add(newevent);
      }
    });
    return events;
  }

日历.dart

class Calendar extends StatefulWidget {
  const Calendar({super.key});

  @override
  State<Calendar> createState() => _CalendarState();
}

class _CalendarState extends State<Calendar> {
  
  _data() {
    List<Event> event = EventController().GetEventsController() as List<Event>;
    return event;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: double.infinity,
      child: Column(
        children: [
          Container(
            height: 700,
            margin: EdgeInsets.all(12),
            width: double.infinity,
            child: SfCalendar(
              view: CalendarView.month,
              dataSource: EventDatasource(_data()),
              initialSelectedDate: DateTime.now(),
            ),
          ),
          
        ],
      ),
    );
  }
}

eventDataSource.dart


class EventDatasource extends CalendarDataSource {
  EventDatasource(List<Event> appointments) {
    this.appointments = appointments;
  }
  Event getEvent(int index) => appointments![index];

  @override
  DateTime getStartTime(int index) {
    return getEvent(index).from;
  }

  @override
  DateTime getEndTime(int index) {
    // TODO: implement getEndTime
    return getEvent(index).to;
  }

  @override
  String getSubject(int index) {
    // TODO: implement getSubject
    return getEvent(index).title;
  }

  @override
  String? getNotes(int index) {
    // TODO: implement getNotes
    return getEvent(index).description;
  }
}

我收到此错误

_TypeError(type Future<List<Event>> is not a subtype of type List<Event> in type cast

flutter firebase dart firebase-realtime-database controller
1个回答
0
投票

您收到此错误是因为在您的控制器中您正在返回列表的未来。

在你的calendar.dart中改变这个方法

_data() {
    List<Event> event = EventController().GetEventsController() as List<Event>;
    return event;
  }

到此

_data() async {
    List<Event> event = await EventController().GetEventsController();
    return event;
  }
© www.soinside.com 2019 - 2024. All rights reserved.