title: Firebase Cloud Firestore get array of maps in Flutter
body: <p>I'm trying to build something in Flutter using Firebase Cloud Firestore as my database. I have a collection ('students') which contains a document of each student.</p>
  <p>Within each student document there is an array which contains a map of marks for the student. See below.</p>
  <p><a href="https://i.stack.imgur.com/JGaDx.png" rel="noreferrer"><img src="https://i.stack.imgur.com/JGaDx.png" alt="Screenshot from Firebase Cloud Firestore document with an array (grades) of maps (various fields relating to the marks a student has received)" /></a></p>
  <p>In Flutter I have the following classes:</p>
  <pre><code>class Student {
    final String name;
    final String subject;
    final List&lt;Mark&gt; marks;
  
    Student({this.name, this.subject, this.marks});
  }
  
  class Mark {
    final int mark;
    final String grade;
    final String markedBy;
    final String feedback;
  
    Mark({this.mark, this.grade, this.markedBy, this.feedback});
  }
  
  </code></pre>
  <p>I'm retrieving the data using a Provider stream as follows:</p>
  <pre><code>  Stream&lt;List&lt;Student&gt;&gt; get firebaseStudents {
      return Firestore.instance.collection('students').snapshots().map(_firebaseStudentsFromSnapshot);
    }
  
    List&lt;Student&gt; _firebaseStudentsFromSnapshot(QuerySnapshot snapshot) {
      return snapshot.documents.map((doc) {
        return Student(
          name: doc.data['name'] ?? '',
          subject: doc.data['subject'] ?? '',
  
          //trying to figure out how to map the marks!
  
        );
      }).toList();
    }
  
  </code></pre>
  <p>If I print doc.data['grades'] I can see it is an array of maps with the data in, I'm just not sure how to then map that to a list of the class Mark.</p>
  <p>I've looked around and seen suggestions such as using fromMap but can't seem to get it working. I've tried the below (in Mark class):</p>
  <pre><code>  Mark.fromMap(Map&lt;String,dynamic&gt; map) : 
      mark = map['mark'], 
      grade = map['grade'],
      markedBy = map['markedBy'],
      feedback = map['feedback'];
  </code></pre>
  <p>And in the database retrieve:</p>
  <pre><code>  marks: List&lt;Mark&gt;.from(doc.data['grades'].map((item) {
      return new Mark(
          mark: item['mark'],
          grade: item['grade'],
          markedBy = item['markedBy'],
          feedback = map['feedback']
         );
       })),
  </code></pre>
  <p>But to no avail. I'm sure I'm just missing a key part but I'm still fairly new to this so I'm trying to get my head around all of this.</p>
  <p>Thanks in advance for any help or advice.</p>
  <p><strong>Edit</strong></p>
  <p>Ultimately figured something out that works but always happy to hear if there are better ways of doing this.</p>
  <p>I presume there probably is a smarter way of doing it using a fromMap but currently the type differences are causing me a problem but will see if I can get that working as that would be a bit neater.</p>
  <pre><code>  List&lt;Student&gt; _firebaseStudentsFromSnapshot(QuerySnapshot snapshot) {
      return snapshot.documents.map((doc) {
        List&lt;Mark&gt; marks = [];
        List&lt;dynamic&gt; markMap = doc.data['grades'];
        markMap.forEach((element) {
          marks.add(new Mark(
              mark = element['mark'], 
              grade = element['grade'],
              markedBy = element['markedBy'],
              feedback = element['feedback'];
          ));
        });
        return Student(
          name: doc.data['name'] ?? '',
          subject: doc.data['subject'] ?? '',
          marks: marks,
        );
      }).toList();
    }
  </code></pre>