简体   繁体   中英

How to retrieve all documents in a collection in Firebase and add to a list?

I have a collection in Firebase that I am trying to retrieve and add to a list:事件

I also have an events model defined. Before adding the event to a list, I would like to create an Event object using the data read from Firebase.

event_model:

class Event {
  String eid;
  String title;
  String location;
  String start;
  String end;
  String instructor;
  String image;
  String description;

  Event({
    required this.eid,
    required this.title,
    required this.location,
    required this.start,
    required this.end,
    required this.instructor,
    required this.image,
    required this.description
  });

  String getEid() {
    return eid;
  }

  String getTitle() {
    return title;
  }

  String getLocation() {
    return location;
  }

  String getStart() {
    return start;
  }

  String getEnd() {
    return end;
  }

  String getInstructor() {
    return instructor;
  }

  String getImage() {
    return image;
  }

  String getDescription() {
    return description;
  }

  void setEid(String eid) {
    this.eid = eid;
  }

  void setTitle(String title) {
    this.title = title;
  }

  void setLocation(String location) {
    this.location = location;
  }

  void setStart(String start) {
    this.start = start;
  }

  void setEnd(String end) {
    this.end = end;
  }

  void setInstructor(String instructor) {
    this.instructor = instructor;
  }

  void setImage(String image) {
    this.image = image;
  }

  void setDescription(String description) {
    this.description = description;
  }
}

This is what I have so far. I am creating the list of Event objects then trying to get the entire collection and for each document in the collection, I am creating the Event object and trying to add it to the list. I am not sure if this is correct.

List<Event> _events = [];

  Future<UserProfile> getUserProfile() async {
    try {
      final FirebaseAuth auth = FirebaseAuth.instance;

      final snapshot = await FirebaseFirestore.instance.collection('events').get();
      snapshot.docs.forEach((doc) {
        Map<String, dynamic>? data = snapshot.data();
        Event event = Event(
              eid: data?['eid'],
              title: data?['title'],
              ...
      });

a better approach for this is that the conversation of the Map<String, dynamic> to an Event class object, should be done using a factory constructor of the Event class, and setting a default value for each property so if something goes null, your app won't crash, it will have a default value and work fine, like this:

add this to your Event class:

 factory Event.fromMap(Map<String, dynamic>? map) {
return Event(
  eid:  map?["eid"] ?? "defaultValue,"
  title:  map?["title"] ?? "defaultValue",
  location:  map?["location"] ?? "defaultValue",
  start:  map?["start"] ?? "defaultValue,"
  end:  map?["ends"] ?? "defaultValue,"
  instructor:  map?["instructor"] ?? "defaultValue,"
  image:   map?["image"] ?? "defaultValue,"
  description:  map?["description"] ?? "defaultValue",
);
 }

then instead of implementing your methods, save yourself from the boilerplate code by using the:

 Event event = Event.fromMap(snapshot.data() as Map<String, dynamic>);
 _events.add(event);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM