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
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>andSet<E>convertible, each element converted toEwith this service.override -
addConverter<
S, T> (T? converter(S source)) → void -
Adds
converterfromSto exactlyT; it also serves the nullable targetT?.override -
addEnum<
E extends Enum> (List< E> values) → void -
Makes the enum
Econvertible: fromString, back toString(its name), and asList<E>orSet<E>(see addCollectionConverters).override -
addMapConverters<
K, V> () → void -
Makes
Map<K, V>convertible from any map, each key converted toKand each value toVwith this service (Spring'sMapToMapConverter).override -
canConvert(
Object? source, Type targetType) → bool -
Whether
sourcecan be converted totargetType: it already is one, or a converter fortargetTypeaccepts it. Anullsource is always convertible.override -
canConvertType<
S> (Type targetType) → bool -
Whether values of type
Scan be converted totargetType, checked before any value exists (for example when wiring code is generated).override -
convert<
T> (Object? source) → T -
Converts
sourcetoT, the usual call.override -
convertTo(
Object? source, Type targetType) → Object? -
Converts
sourcetotargetType, 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
TandT?, whatever its source, for example to replace a default converter. A value that already is aTstill converts to itself.override -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited