propertyFromJson static method

String propertyFromJson(
  1. ModelProperty property
)

Implementation

static String propertyFromJson(ModelProperty property) {
  if (property.category == TypeCategory.enumeration) {
    final optionalMarker = property.isOptional ? 'firstOrNull' : 'first';
    return "${property.type}.values.where((e) => e.name == json['${property.originalName}']).$optionalMarker";
  }

  final optionalMarker =
      property.isOptional && property.type != 'dynamic' ? '?' : '';

  if (property.type == 'double') {
    return 'json[\'${property.originalName}\']$optionalMarker.toDouble()';
  }

  if (property.isList) {
    final castCall = '${property.type.replaceAll('List', 'cast')}()';
    return 'json[\'${property.originalName}\']$optionalMarker.$castCall';
  }

  return 'json[\'${property.originalName}\'] as ${property.type}$optionalMarker';
}