title: Write and Read nested Map to Firebase Document from Flutter
body: <p>I am struggling to write (and also haven't yet figured out how to read) nested maps in firebase from my flutter app. I'm writing an expense tracker where the list of categories is stored within each logbook. I can map, save, and retrieve the primitive fields to and from firebase, where I get lost is trying to write the map of the categories and subcategories to firebase and then how to read them.</p>
  
  <p>Solving for both categories and subcategories is essentially the same so I'll just present one of them. Also, I currently have the category Id as both the key and part of the category class itself, I'll be deleting the Id from the class itself later, as I believe its bad practice, it's just helping me at the moment elsewhere.</p>
  
  <p>I've also been following the BLOC method, so I also have models that convert to entities for dealing with firebase. </p>
  
  <p>This is a category (I've removed irrelevant info such as props and to string):</p>
  
  <pre><code>&lt;pre&gt;
      class MyCategory extends Equatable {
    final String id;
    final String name;
    final IconData iconData;
  
    MyCategory({@required this.id, @required this.name, this.iconData});
    MyCategoryEntity toEntity() {
      return MyCategoryEntity(
        id: id,
        name: name,
        iconCodePoint: iconData.codePoint.toString(),
        iconFontFamily: iconData.fontFamily,
      );
    }
  
    static MyCategory fromEntity(MyCategoryEntity entity) {
      return MyCategory(
        id: entity.id,
        name: entity.name,
        iconData: IconData(int.parse(entity.iconCodePoint),
            fontFamily: entity.iconFontFamily),
      );
    }
  }
  &lt;/pre&gt;
  </code></pre>
  
  <p>The model entity for the category has JsonSerialization and also uses the Icon codepoint/fontFamily as it seems to be more friendly to pass back and forth from firebase than IconData directly.</p>
  
  <pre><code>&lt;pre&gt;
      @JsonSerializable()
  class MyCategoryEntity extends Equatable {
    final String id;
    final String name;
    final String iconCodePoint;
    final String iconFontFamily;
  
    const MyCategoryEntity(
        {this.id, this.name, this.iconCodePoint, this.iconFontFamily});
  
    factory MyCategoryEntity.fromJson(Map&lt;String, dynamic&gt; json) =&gt;
        _$MyCategoryEntityFromJson(json);
  
    Map&lt;String, dynamic&gt; toJson() =&gt; _$MyCategoryEntityToJson(this);
  &lt;/pre&gt;
  </code></pre>
  
  <p>I don't think you need to see the Log model as its more the LogEntity that speaks directly to firebase. This is where I'm completely stuck. I've seen a few examples of how to pass a list of objects, but I can't seem to figure out how to pass a map of objects. I'm also not sure how I'd read it back into the LogEntity from firestore (as can be seen in my two TODOs down below).</p>
  
  <pre><code>&lt;pre&gt;
      class LogEntity extends Equatable {
    final String uid;
    final String id;
    final String logName;
    final String currency;
    final bool active;
    //String is the category id
    final Map&lt;String, MyCategory&gt; categories;
    final Map&lt;String, MySubcategory&gt; subcategories;
  
  
    const LogEntity({this.uid, this.id, this.logName, this.currency, this.categories, this.subcategories, this.active});
  
  
    static LogEntity fromSnapshot(DocumentSnapshot snap) {
      return LogEntity(
        uid: snap.data[UID],
        id: snap.documentID,
        logName: snap.data[LOG_NAME],
        currency: snap.data[CURRENCY_NAME],
        //TODO de-serialize categories and subcategories
        active: snap.data[ACTIVE],
  
      );
    }
  
    Map&lt;String, Object&gt; toDocument() {
      return {
        UID: uid,
        LOG_NAME: logName,
        CURRENCY_NAME: currency,
      //TODO serialize categories and subcategories, this does doesn't work
        CATEGORIES: categories.map((key, value) =&gt; categories[key].toEntity().toJson()).toString(),
        ACTIVE: active,
      };
    }
  }
  &lt;/pre&gt;
  </code></pre>
  
  <p>I'm really hoping for some help on this as the only examples I've been able to understand are lists, not maps. Also I want to use a similar method in a few places in this app, so figuring this out is critical.</p>
  
  <p>Git: <a href="https://github.com/cver22/expenses2" rel="noreferrer">https://github.com/cver22/expenses2</a></p>