asMap method Null safety

  1. @override
Map<String, dynamic> asMap()
override

Converts this instance into a serializable map.

This method returns a map of the key-values pairs in this instance. This value is typically converted into a transmission format like JSON.

Only properties present in backing are serialized, otherwise, they are omitted from the map. If a property is present in backing and the value is null, the value null will be serialized for that property key.

Usage: var json = json.encode(model.asMap());

Implementation

@override
Map<String, dynamic> asMap() {
  var outputMap = <String, dynamic>{};

  backing.contents!.forEach((k, v) {
    if (!_isPropertyPrivate(k!)) {
      outputMap[k] = entity.properties[k]!.convertToPrimitiveValue(v);
    }
  });

  entity.attributes.values
      .where((attr) => attr!.transientStatus?.isAvailableAsOutput ?? false)
      .forEach((attr) {
    var value = entity.runtime!.getTransientValueForKey(this, attr!.name);
    if (value != null) {
      outputMap[attr.name] = value;
    }
  });

  return outputMap;
}