Converter class

Fluent wrapper for chained type conversions with navigation and fallbacks.

Converter provides a chainable API for extracting and converting values from nested data structures. It is the primary way to convert values fluently via the value.convert.toInt() syntax.

Basic Usage

final json = {'user': {'age': '25'}};
final age = json.convert.fromMap('user').fromMap('age').toInt();

With Defaults

final value = data.convert.withDefault(0).toInt(); // Falls back to 0

With Custom Pre-Processing

final value = raw.convert.withConverter((v) => v?.trim()).string();

Unlike Convert, which is stateless, Converter wraps a value and allows incremental navigation through nested structures. Configuration set via withDefault or withConverter persists through navigation methods.

See also:

  • Convert for stateless conversion methods.
  • ConvertObjectExtension for the .convert getter.

Constructors

Converter(Object? _value, {Object? defaultValue, DynamicConverter? customConverter})
Creates a converter wrapping _value with optional fallback and pre-processing.
const

Properties

decoded Converter
Explicitly decodes a JSON string into a Dart object (Map, List, etc.).
no setter
hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

fromList(int index) Converter
Extracts a value from a List using the specified index.
fromMap(Object? key) Converter
Extracts a value from a Map using the specified key.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
string({dynamic mapKey, int? listIndex, String? defaultValue, DynamicConverter<String>? converter}) String
Converts to String, mirroring Convert.string.
to<T>() → T
Converts the wrapped value to type T.
toBigInt({dynamic mapKey, int? listIndex, BigInt? defaultValue, DynamicConverter<BigInt>? converter}) BigInt
Converts to BigInt, mirroring Convert.toBigInt.
toBigIntOr(BigInt defaultValue, {dynamic mapKey, int? listIndex, DynamicConverter<BigInt>? converter}) BigInt
Converts to BigInt, falling back to defaultValue on failure.
toBool({dynamic mapKey, int? listIndex, bool? defaultValue, DynamicConverter<bool>? converter}) bool
Converts to bool, mirroring Convert.toBool.
toBoolOr(bool defaultValue, {dynamic mapKey, int? listIndex, DynamicConverter<bool>? converter}) bool
Converts to bool, falling back to defaultValue on failure.
toDateTime({dynamic mapKey, int? listIndex, String? format, String? locale, bool autoDetectFormat = false, bool useCurrentLocale = false, bool utc = false, DateTime? defaultValue, DynamicConverter<DateTime>? converter}) DateTime
Converts to DateTime, mirroring Convert.toDateTime.
toDateTimeOr(DateTime defaultValue, {dynamic mapKey, int? listIndex, String? format, String? locale, bool autoDetectFormat = false, bool useCurrentLocale = false, bool utc = false, DynamicConverter<DateTime>? converter}) DateTime
Converts to DateTime, falling back to defaultValue on failure.
toDouble({dynamic mapKey, int? listIndex, String? format, String? locale, double? defaultValue, DynamicConverter<double>? converter}) double
Converts to double, mirroring Convert.toDouble.
toDoubleOr(double defaultValue, {dynamic mapKey, int? listIndex, String? format, String? locale, DynamicConverter<double>? converter}) double
Converts to double, falling back to defaultValue on failure.
toEnum<T extends Enum>({required T parser(dynamic), dynamic mapKey, int? listIndex, T? defaultValue, Map<String, dynamic>? debugInfo}) → T
Converts to T using parser, mirroring Convert.toEnum.
toInt({dynamic mapKey, int? listIndex, String? format, String? locale, int? defaultValue, DynamicConverter<int>? converter}) int
Converts to int, mirroring Convert.toInt.
toIntOr(int defaultValue, {dynamic mapKey, int? listIndex, String? format, String? locale, DynamicConverter<int>? converter}) int
Converts to int, falling back to defaultValue when conversion fails.
toList<T>({dynamic mapKey, int? listIndex, List<T>? defaultValue, DynamicConverter<T>? elementConverter}) List<T>
Converts to List, optionally transforming each item.
toMap<K, V>({dynamic mapKey, int? listIndex, Map<K, V>? defaultValue, DynamicConverter<K>? keyConverter, DynamicConverter<V>? valueConverter}) Map<K, V>
Converts to Map, allowing converters for keys and values.
toNum({dynamic mapKey, int? listIndex, String? format, String? locale, num? defaultValue, DynamicConverter<num>? converter}) num
Converts to num, mirroring Convert.toNum.
toNumOr(num defaultValue, {dynamic mapKey, int? listIndex, String? format, String? locale, DynamicConverter<num>? converter}) num
Converts to num, falling back to defaultValue on conversion failure.
toOr<T>(T defaultValue) → T
Converts the wrapped value to type T, falling back to defaultValue on failure.
toSet<T>({dynamic mapKey, int? listIndex, Set<T>? defaultValue, DynamicConverter<T>? elementConverter}) Set<T>
Converts to Set, optionally transforming each item.
toString() String
Overrides Object.toString using the conversion logic.
override
toStringOr(String defaultValue, {dynamic mapKey, int? listIndex, DynamicConverter<String>? converter}) String
Converts to String, falling back to defaultValue when conversion fails.
toUri({dynamic mapKey, int? listIndex, Uri? defaultValue, DynamicConverter<Uri>? converter}) Uri
Converts to Uri, mirroring Convert.toUri.
toUriOr(Uri defaultValue, {dynamic mapKey, int? listIndex, DynamicConverter<Uri>? converter}) Uri
Converts to Uri, falling back to defaultValue on failure.
tryTo<T>() → T?
Attempts to convert the wrapped value to type T.
tryToBigInt({dynamic mapKey, int? listIndex, BigInt? defaultValue, DynamicConverter<BigInt>? converter}) BigInt?
Converts to BigInt without throwing, mirroring Convert.tryToBigInt.
tryToBool({dynamic mapKey, int? listIndex, bool? defaultValue, DynamicConverter<bool>? converter}) bool?
Converts to bool without throwing, mirroring Convert.tryToBool.
tryToDateTime({dynamic mapKey, int? listIndex, String? format, String? locale, bool autoDetectFormat = false, bool useCurrentLocale = false, bool utc = false, DateTime? defaultValue, DynamicConverter<DateTime>? converter}) DateTime?
Converts to DateTime without throwing, mirroring Convert.tryToDateTime.
tryToDouble({dynamic mapKey, int? listIndex, String? format, String? locale, double? defaultValue, DynamicConverter<double>? converter}) double?
Converts to double without throwing, mirroring Convert.tryToDouble.
tryToEnum<T extends Enum>({required T parser(dynamic), dynamic mapKey, int? listIndex, T? defaultValue, Map<String, dynamic>? debugInfo}) → T?
Converts to T without throwing, mirroring Convert.tryToEnum.
tryToInt({dynamic mapKey, int? listIndex, String? format, String? locale, int? defaultValue, DynamicConverter<int>? converter}) int?
Converts to int without throwing, mirroring Convert.tryToInt.
tryToList<T>({dynamic mapKey, int? listIndex, List<T>? defaultValue, DynamicConverter<T>? elementConverter}) List<T>?
Converts to List without throwing, returning null when conversion fails.
tryToMap<K, V>({dynamic mapKey, int? listIndex, Map<K, V>? defaultValue, DynamicConverter<K>? keyConverter, DynamicConverter<V>? valueConverter}) Map<K, V>?
Converts to Map without throwing, returning null on failure.
tryToNum({dynamic mapKey, int? listIndex, String? format, String? locale, num? defaultValue, DynamicConverter<num>? converter}) num?
Converts to num without throwing, mirroring Convert.tryToNum.
tryToSet<T>({dynamic mapKey, int? listIndex, Set<T>? defaultValue, DynamicConverter<T>? elementConverter}) Set<T>?
Converts to Set without throwing, returning null on failure.
tryToString({dynamic mapKey, int? listIndex, String? defaultValue, DynamicConverter<String>? converter}) String?
Converts to String without throwing, mirroring Convert.tryToString.
tryToUri({dynamic mapKey, int? listIndex, Uri? defaultValue, DynamicConverter<Uri>? converter}) Uri?
Converts to Uri without throwing, mirroring Convert.tryToUri.
withConverter(DynamicConverter converter) Converter
Applies a custom transformation to the value before any conversion attempt.
withDefault(Object? value) Converter
Configures a default value to be returned if the subsequent conversion operation fails.

Operators

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