tryGetAsCode function

String? tryGetAsCode(
  1. Object? value
)

Implementation

String? tryGetAsCode(Object? value) {
  if (value is String) {
    final escaped = escapeString(value);
    return escaped;
  } else if (value is bool) {
    return '$value';
  } else if (value is num) {
    return '$value';
  } else if (value == null) {
    return '$value';
  } else if (value is Enum) {
    return '${value.runtimeType}.${value.name}';
  } else if (value is List) {
    final values = [];
    for (var item in value) {
      final code = tryGetAsCode(item);
      values.add(code);
    }

    return '[${values.join(', ')}]';
  } else if (value is Set) {
    final values = [];
    for (var item in value) {
      final code = tryGetAsCode(item);
      values.add(code);
    }

    return '{${values.join(', ')}}';
  } else if (value is Map) {
    final values = [];
    for (var key in value.keys) {
      final k = tryGetAsCode(key);
      final v = tryGetAsCode(value[key]);
      values.add('$k: $v');
    }

    return '{${values.join(', ')}}';
  }

  return null;
}