retrieveModelFromJson method

Future<ModelEntity> retrieveModelFromJson(
  1. dynamic json,
  2. Map<String, ModelEntity> models
)

Implementation

Future<ModelEntity> retrieveModelFromJson(
    dynamic json, Map<String, ModelEntity> models) async {
  var fields = [...this.fields];
  if (json is Map<String, dynamic>) {
    final futures = <Future<ModelFieldEntity?>>[];
    for (final key in json.entries) {
      futures.add(
        _retrieveField(
          _GenFieldParams(
            entry: key,
            fields: fields,
            models: models,
          ),
        ),
      );
    }
    final res = await Future.wait(
      futures,
      eagerError: false,
    );
    return copyWith(
      fields: res.whereNotNull().toList(),
    );
  } else if (json is List<dynamic>) {
    final firstElement = json.firstOrNull;
    if (firstElement == null) {
      return copyWith(fields: fields);
    }
    if (firstElement is Map<String, dynamic>) {
      final futures = <Future<ModelFieldEntity?>>[];
      for (final e in firstElement.entries) {
        futures.add(
          _retrieveField(
            _GenFieldParams(
              entry: e,
              fields: fields,
              models: models,
            ),
          ),
        );
      }
      final res = await Future.wait(
        futures,
        eagerError: false,
      );
      copyWith(
        fields: res.whereNotNull().toList(),
      );
    }
  }
  return copyWith(fields: fields);
}