ConversionResult<T> class

A monadic wrapper representing the outcome of a type conversion operation.

Use ConversionResult when you need to capture both success and failure states without immediately throwing exceptions. This is useful for:

  • Batch processing where you want to collect all errors before reporting.
  • Functional-style pipelines using map and flatMap.
  • Deferred error handling via fold.

Example

final result = ConversionResult.success(42);
final doubled = result.map((v) => v * 2);
print(doubled.value); // 84

See also: ConversionException for the error type captured on failure.

Constructors

ConversionResult.failure(ConversionException error)
Creates a failed result containing error.
factory
ConversionResult.success(T value)
Creates a successful result containing value.
factory

Properties

error ConversionException?
The captured ConversionException if this is a failure, otherwise null.
no setter
hashCode int
The hash code for this object.
no setterinherited
isFailure bool
Returns true if this result contains an error.
no setter
isSuccess bool
Returns true if this result contains a successfully converted value.
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
value → T
Returns the converted value, throwing ConversionException on failure.
no setter
valueOrNull → T?
Returns the converted value when successful, or null on failure.
no setter

Methods

flatMap<R>(ConversionResult<R> next(T value)) ConversionResult<R>
Chains another conversion operation when this result is successful.
fold<R>({required R onSuccess(T value), required R onFailure(ConversionException error)}) → R
Reduces this result to a single value by handling both outcomes explicitly.
map<R>(R transform(T value)) ConversionResult<R>
Transforms the contained value using transform when successful.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
inherited
valueOr(T defaultValue) → T
Returns the converted value when successful, or defaultValue on failure.

Operators

operator ==(Object other) bool
The equality operator.
inherited