How do I pull an array from a Firestore document and convert it into a list of objects in flutter?

In my document, I have an array of notes. Each note has the writer, the note text, etc. I wrote the notes using a class called Note. I would like to pull just the notes from the document and convert them into a List of Notes.

Here is the data structure for the Note:

Here is the Note class, with its converter .fromFirestore:

class Note {

  String? filingId;
  final String name;
  final String note; 
  final String writerId;
  Timestamp? noteDate;
  
  Note ({
    required this.filingId,
    required this.name,
    required this.writerId,
    required this.note,
    required this.noteDate,
    });

  Map<String, dynamic> toFirestore() {
    return {
      'name': name,
      'writerId': writerId,
      'note': note,
      'noteDate': noteDate
    };
  }

  factory Note.fromFirestore(
    DocumentSnapshot<Map<String, dynamic>> snapshot,
    SnapshotOptions? options,) {
      final data = snapshot.data();
      return Note(
        noteDate: data?['noteDate'], 
        name: data?['name'],
        writerId: data?['writerId'], 
        note: data?['note'], 
        filingId: snapshot.id);
    }

}

And here is the FilingData class, which includes the notes as an array. It also has a converter .fromFiretore.

class FilingData {

  // This is the data for an entry in the timeline.
  Timestamp? createDate;
  String? filingId;
  final String jurisdiction; 
  final String filingName;
  String? frequency;
  Timestamp? nextDueDate;
  final String method;
  String? autoAssign;
  List<dynamic>? notes;
  Timestamp? updateDate;
  
  FilingData ({
    required this.createDate,
    filingId,
    required this.jurisdiction,
    required this.filingName,
    required this.frequency,
    required this.nextDueDate,
    required this.method,
    required this.autoAssign,
    notes,
    required this.updateDate,
    });    

factory FilingData.fromFirestore(
        DocumentSnapshot<Map<String, dynamic>> snapshot,
        SnapshotOptions? options,) {
          final data = snapshot.data();
          return FilingData(
            filingId: snapshot.id,
            createDate: data?['createDate'], 
            jurisdiction: data?['jurisdiction'],
            filingName: data?['filingName'], 
            frequency: data?['frequency'],
            nextDueDate: data?['nextDueDate'], 
            method: data?['method'],
            autoAssign: data?['autoAssign'],
            updateDate: data?['updateDate'],
            notes: data?['notes'] is Iterable ? List.from(data?['notes']) : null);
        }

Here is my current code to get the notes. But it does not work. First, I try to get the entire FilingData object. Then I try to pull the notes using dot-notation. Debugging shows that notesList (where I try to pull the notes using dot-notation) is null. So I’m guessing my method using dot-notation does not work. How should I do it instead? And do I have to get the full FilingData (which includes the notes array) or can I just pull the notes array?

Future<List<Note>> getAllNotes(String filingId) async {
    var ref =  _db.collection('filings').doc(filingId).withConverter(
      fromFirestore: FilingData.fromFirestore, 
      toFirestore: (FilingData, _) => FilingData.toFirestore(),
    );

    final docSnap = await ref.get();
    var notesList = docSnap.data()?.notes;
    List<Note> allNotes = [];
    // for (int i = 0; i < notesList!.length; i++) {
    //   Note thisNote = Note(name: notesList[i].name, appUserId: notesList[i].appUserId, note: notesList[i].note, noteDate: notesList[i].noteDate);
    //   allNotes.add(thisNote);
    // }
    return allNotes;
}

So after working on it further, I was able to do it. But it’s not very elegant. First, I noticed that the FilingData class did not require the notes array. So I made that required. The following code now works:

FilingData ({
        required this.createDate,
        filingId,
        required this.jurisdiction,
        required this.filingName,
        required this.frequency,
        required this.nextDueDate,
        required this.method,
        required this.autoAssign,
        required this.notes,
        required this.updateDate,
        });  
    
    
    
      Future<List<Note>> getAllNotes(String filingId) async {
        var ref =  _db.collection('filings').doc(filingId).withConverter(
          fromFirestore: FilingData.fromFirestore, 
          toFirestore: (FilingData, _) => FilingData.toFirestore(),
        );
    
        final docSnap = await ref.get();
        List<Note> allNotes = [];
        // Check to see if there are any notes...
        if (docSnap.data()?.notes != null) {
        List? notesList = docSnap.data()?.notes!.toList();    
        for (int i = 0; i < notesList!.length; i++) {
          
          Note thisNote = Note(
            filingId: filingId, 
            name: notesList[i]["name"], 
            writerId: notesList[i]["writerId"], 
            note: notesList[i]["note"], 
            noteDate: notesList[i]["noteDate"]);
    
          allNotes.add(thisNote);
        }
        }
        return allNotes;
    }

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật