matches method

  1. @override
bool matches(
  1. Class sourceType,
  2. Class targetType
)
override

A contract for a converter that only matches under certain conditions.

This is useful in frameworks where type conversion should occur only if some custom logic (e.g., annotations, context, runtime checks) validates it.

You can implement this interface when you want to provide more granular control over when a converter should be applied.


🔧 Example:

class MyConditionalConverter implements ConditionalConverter {
  @override
  bool matches(Class sourceType, Class targetType) {
    return sourceType.getType() == String && targetType.getType() == Uri;
  }
}

Determines if the converter should apply for the given sourceType and targetType.

Implementation

@override
bool matches(Class sourceType, Class targetType) {
  if (sourceType.getType() != _source.getType() || !_isSupportedTarget(targetType.getType())) {
    return false;
  }

  final sourceElementType = sourceType.componentType();
  final targetElementType = targetType.componentType();

  if (sourceElementType == null || targetElementType == null) {
    // Allow if we can't determine element types — rely on runtime conversion
    return true;
  }

  return ConversionUtils.canConvert(sourceElementType, targetElementType, _conversionService);
}