encode static method

String encode(
  1. Map<String, Object?> values, {
  2. Map<String, String> types = const {},
})

Encodes values in an explicitly typed envelope.

Implementation

static String encode(
  Map<String, Object?> values, {
  Map<String, String> types = const {},
}) => jsonEncode({
  'format': _format,
  'values': copyPreferences(values).map(
    (key, value) => MapEntry(
      key,
      types[key] == 'double' && value is num
          ? ['double', value.toDouble().toString()]
          : switch (value) {
              String() => ['string', value],
              bool() => ['bool', value],
              int() => ['int', value.toString()],
              double() => ['double', value.toString()],
              List<String>() => ['list', value],
              _ => throw StateError('Unsupported preference type.'),
            },
    ),
  ),
});