canConvert static method
Checks if a conversion from source
to target
is possible using the given conversionService
.
- If
target
isnull
, conversion is considered allowed. - If
source
isnull
, 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;
}