readOption<T> method

T? readOption<T>(
  1. T readValue()
)

Decodes an optional value

Format: bool discriminant (false = None, true = Some) + value if Some

Returns null if the discriminant is false, otherwise returns the decoded value.

Example:

final maybeText = decoder.readOption<String>(() => decoder.readString());
if (maybeText != null) {
  print('Got: $maybeText');
}

Implementation

T? readOption<T>(T Function() readValue) {
  final tag = readU8();
  if (tag == 0) return readValue();
  if (tag == 1) return null;
  throw SpacetimeDbProtocolException(
    'Invalid Option tag: $tag (expected 0=some or 1=none)',
  );
}