GenericConversionService class

A conversion service that starts empty and converts only what is registered on it.

It is the working implementation of ConversionService and ConverterRegistry; DefaultConversionService is this class with the standard converters already added. Use it directly when you want full control, for example a service that knows only your own types, or in a test. Most code uses DefaultConversionService instead.

Converters are looked up by the exact target Type. Among converters for the same target, the newest registration whose source type matches (with is) wins. A value that already is of the target type is returned unchanged.

// Prices travel as text in messages; this service only knows money.
final service = GenericConversionService()
  ..addConverter<String, Money>(Money.parse)
  ..addConverter<Money, String>((money) => money.format());
final price = service.convert<Money>('9.99 EUR');

Spring (Java):

GenericConversionService service = new GenericConversionService();
service.addConverter(String.class, Money.class, Money::parse);
service.addConverter(Money.class, String.class, Money::format);
Money price = service.convert("9.99 EUR", Money.class);

Differences: there is no ConverterFactory or GenericConverter, and no search through the class hierarchy of the target, because Dart has no run-time type introspection. Register each concrete target instead.

Implemented types

Constructors

GenericConversionService()

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

addCollectionConverters<E>() → void
Makes List<E> and Set<E> convertible, each element converted to E with this service.
override
addConverter<S, T>(T? converter(S source)) → void
Adds converter from S to exactly T; it also serves the nullable target T?.
override
addEnum<E extends Enum>(List<E> values) → void
Makes the enum E convertible: from String, back to String (its name), and as List<E> or Set<E> (see addCollectionConverters).
override
addMapConverters<K, V>() → void
Makes Map<K, V> convertible from any map, each key converted to K and each value to V with this service (Spring's MapToMapConverter).
override
canConvert(Object? source, Type targetType) → bool
Whether source can be converted to targetType: it already is one, or a converter for targetType accepts it. A null source is always convertible.
override
canConvertType<S>(Type targetType) → bool
Whether values of type S can be converted to targetType, checked before any value exists (for example when wiring code is generated).
override
convert<T>(Object? source) → T
Converts source to T, the usual call.
override
convertTo(Object? source, Type targetType) → Object?
Converts source to targetType, for callers that know the target only as a Type value at run time, such as a binder walking fields.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
removeConvertible<T>() → void
Removes every converter targeting T and T?, whatever its source, for example to replace a default converter. A value that already is a T still converts to itself.
override
toString() → String
A string representation of this object.
inherited

Operators

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