toMap method

Map<String, dynamic> toMap({
  1. bool forModelJson = false,
})

Convert this to a string map. Set forModelJson to exclude some keys that should not be part of the model JSON file.

Note: this is used by EntityResolver for model persistence in cache files and indirectly by CodeBuilder when generating objectbox-model.json.

So the order of writing keys matters, it defines the order in the final JSON.

Implementation

Map<String, dynamic> toMap({bool forModelJson = false}) {
  final ret = <String, dynamic>{};
  ret[ModelEntityKey.id] = id.toString();
  ret[ModelEntityKey.lastPropertyId] = lastPropertyId.toString();
  ret[ModelEntityKey.name] = name;
  if (externalName != null) ret[ModelEntityKey.externalName] = externalName;
  if (flags != 0) ret[ModelEntityKey.flags] = flags;
  ret[ModelEntityKey.properties] =
      properties.map((p) => p.toMap(forModelJson: forModelJson)).toList();
  ret[ModelEntityKey.relations] =
      relations.map((r) => r.toMap(forModelJson: forModelJson)).toList();
  if (!forModelJson) {
    ret[ModelEntityKey.backlinks] = backlinks.map((r) => r.toMap()).toList();
    ret[ModelEntityKey.constructorParams] = constructorParams;
    ret[ModelEntityKey.uidRequest] = uidRequest;
  }
  return ret;
}