TypeRegistry class

Registry of custom parsers for application-specific types.

Use TypeRegistry to extend Convert.toType and Convert.tryToType with support for your own domain models. Each registered parser receives the raw input and should return an instance of the target type.

Example

class Money {
  final int cents;
  Money(this.cents);
  factory Money.parse(Object? o) => Money(Convert.toInt(o));
}

final config = ConvertConfig(
  registry: const TypeRegistry.empty().register<Money>(Money.parse),
);

ConvertConfig.configure(config);
final money = Convert.toType<Money>('42'); // Money(42)

Parsers should throw to signal failure. The exception is wrapped in ConversionException with full context.

Annotations

Constructors

TypeRegistry.empty()
Creates an empty registry with no custom parsers.
const

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

merge(TypeRegistry other) TypeRegistry
Merges this registry with other, returning a new registry.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
register<T>(T parser(Object?)) TypeRegistry
Returns a new registry with parser registered for type T.
toString() String
A string representation of this object.
inherited
tryParse<T>(Object? value) → T?
Attempts to parse value into T using a registered custom parser.

Operators

operator ==(Object other) bool
The equality operator.
inherited