convert<T> method

T convert<T>(
  1. Snapshot input, {
  2. String? format,
})

Converts input to an object of type T

Throws a StateError when not sealed. Throws a FormatException when no applicable converter registered.

Implementation

T convert<T>(Snapshot input, {String? format}) {
  if (!isSealed) {
    throw StateError('Cannot be used when not sealed.');
  }
  var value = input.value;
  if (value is T) return value;
  var factories = _getConverters<T?>();
  for (var factory in factories.reversed) {
    if (factory.canHandle(input, format)) {
      return factory.create(input, format);
    }
  }
  throw FormatException('Decoding of `$input` to type $T not supported.');
}