fromJson static method

RecordModel fromJson(
  1. Map<String, dynamic> json
)

Implementation

static RecordModel fromJson(Map<String, dynamic> json) {
  final model = _$RecordModelFromJson(json)..expand = {};

  // resolve and normalize the expand item(s) recursively
  (json["expand"] as Map<String, dynamic>? ?? {}).forEach((key, value) {
    final result = <RecordModel>[];

    if (value is Iterable) {
      model._multiExpandKeys.add(key);
      for (final item in value) {
        result.add(RecordModel.fromJson(item as Map<String, dynamic>? ?? {}));
      }
    }

    if (value is Map) {
      model._singleExpandKeys.add(key);
      result.add(RecordModel.fromJson(value as Map<String, dynamic>? ?? {}));
    }

    model.expand[key] = result;
  });

  // attach the dynamic json fields to the model"s `data`
  // ---
  final baseFields = <String>[
    "id",
    "created",
    "updated",
    "collectionId",
    "collectionName",
    "expand",
  ];

  final rest = Map<String, dynamic>.from(json)
    ..removeWhere((key, value) => baseFields.contains(key));

  model.data = rest;

  return model;
}