nativeToValue function

Map<String, Object?> nativeToValue(
  1. Object? value
)

Converts a native Dart value to a google.protobuf.Value JSON map.

Supported types:

  • null{"nullValue": "NULL_VALUE"}
  • num{"numberValue": n}
  • String{"stringValue": s}
  • bool{"boolValue": b}
  • Map<String, Object?>{"structValue": mapToStruct(m)}
  • List{"listValue": {"values": [...]}}

Throws ArgumentError for unsupported types.

Implementation

Map<String, Object?> nativeToValue(Object? value) {
  if (value == null) {
    return {'nullValue': 'NULL_VALUE'};
  }
  if (value is num) {
    return {'numberValue': value};
  }
  if (value is String) {
    return {'stringValue': value};
  }
  if (value is bool) {
    return {'boolValue': value};
  }
  if (value is Map<String, Object?>) {
    return {'structValue': mapToStruct(value)};
  }
  if (value is List) {
    return {
      'listValue': {'values': value.map((e) => nativeToValue(e)).toList()},
    };
  }
  throw ArgumentError(
    'Cannot convert ${value.runtimeType} to google.protobuf.Value',
  );
}