addConverterFactory method

  1. @override
void addConverterFactory(
  1. ConverterFactory factory
)
override

Registers a ConverterFactory that can produce Converter instances for a target type.


🔧 Example

registry.addConverterFactory(StringToEnumConverterFactory());

Implementation

@override
void addConverterFactory(ConverterFactory<dynamic, dynamic> factory) {
  final clazz = factory.getClass(null, factory.getPackageName());
  // We'll try to access the [ConverterFactory] class as an interface first
  List<Class> types = clazz.getDeclaredInterface<ConverterFactory>()?.getTypeParameters() ?? [];

  // If it fails, we'll try to access it as a superclass
  if(types.isEmpty) {
    types = clazz.getDeclaredSuperClass()?.getTypeParameters() ?? [];
  }

  if (types.isEmpty || types.length.isLessThan(2)) {
    throw ConversionException('Unable to determine source type and target type for your '
        'ConverterFactory [${factory.runtimeType}]; does the class parameterize those types?');
  }
  addPairedConverter(_PairedConverterFactoryAdapter(factory, ConvertiblePair(types.getFirst(), types.getLast()), _pd, _convertNullableSource));
}