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() {
  final outputMap = <String, dynamic>{};

  backing.contents!.forEach((k, v) {
    if (!_isPropertyPrivate(k!)) {
      final property = properties[k];
      final value = property!.convertToPrimitiveValue(v);
      if (value == null && !_includeIfNull(property)) {
        return;
      }
      outputMap[mapKeyName(k)] = value;
    }
  });

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

  return outputMap;
}