canConvert static method

bool canConvert(
  1. Class? source,
  2. Class? target,
  3. ConversionService conversionService
)

Checks if a conversion from source to target is possible using the given conversionService.

  • If target is null, conversion is considered allowed.
  • If source is null, conversion is potentially allowed.
  • Otherwise, it checks:
    • If the conversion service can convert between the two descriptors.
    • Or if the underlying Dart types are assignable.

🔧 Example

bool result = ConversionUtils.canConvertElements(
  Class.of(String),
  Class.of(int),
  myConversionService,
);

Implementation

static bool canConvert(Class? source, Class? target, ConversionService conversionService) {
		if (target == null) {
			return true;
		}

		if (source == null) {
			return true;
		}

		if (conversionService.canConvert(source, target)) {
			return true;
		}

		if (source.isInstance(target)) {
			return true;
		}

		return false;
	}