generateModelFromJson static method

ModelEntity generateModelFromJson({
  1. required dynamic json,
  2. required dynamic onModelGenerated(
    1. ModelEntity model
    ),
})

Implementation

static ModelEntity generateModelFromJson(
    {required dynamic json,
    required Function(ModelEntity model) onModelGenerated}) {
  if (json == null) throw Exception('json is null');
  var fields = <ModelFieldEntity>[];

  if (json is! Map<String, dynamic> && json is! List<dynamic>) {
    return _returnModel(fields);
  }

  if (json is Map<String, dynamic>) {
    for (final e in json.entries) {
      fields = _genField(
        entry: e,
        fields: fields,
        onModelGenerated: onModelGenerated,
      );
    }
    return _returnModel(fields);
  }
  if (json is List<dynamic>) {
    final firstElement = json.firstOrNull;
    if (firstElement == null) {
      return _returnModel(fields);
    }
    if (firstElement is Map<String, dynamic>) {
      for (final e in firstElement.entries) {
        fields = _genField(
          entry: e,
          fields: fields,
          onModelGenerated: onModelGenerated,
        );
      }
    }
    return _returnModel(fields);
  }
  return _returnModel(fields);
}