convert_object library

A comprehensive type conversion library for Dart that handles the messy realities of dynamic data from APIs, databases, and user input.

This library provides a fluent API for converting between primitive types, collections, enums, and dates - with extensive support for locale-aware formatting, custom converters, and graceful error handling.

Core Entry Points

  • Convert - Static facade for all conversion methods (e.g., Convert.toInt()).
  • Converter - Fluent wrapper for chained conversions (e.g., value.convert.toInt()).
  • Top-level functions - Convenience aliases like convertToInt, tryConvertToString.

Configuration

Use ConvertConfig to customize parsing behavior globally or per-scope:

  • NumberOptions - Locale-aware number formatting.
  • DateOptions - Date parsing with auto-detection and custom patterns.
  • BoolOptions - Configurable truthy/falsy string tokens.
  • UriOptions - URI coercion and validation policies.
  • TypeRegistry - Register custom parsers for application-specific types.

Error Handling

All toX methods throw ConversionException on failure with rich context. Use tryToX variants for null-returning, exception-free alternatives.

See also:

Classes

BoolOptions
Options that control boolean parsing.
ConversionResult<T>
A monadic wrapper representing the outcome of a type conversion operation.
Convert
Backward-compatible static facade that mirrors the original ConvertObject API.
ConvertConfig
Global and scoped configuration bundle for all convert_object APIs.
Converter
Fluent wrapper for chained type conversions with navigation and fallbacks.
DateOptions
Options that control date/time parsing.
EnumParsers
Factory methods for building resilient enum parsing callbacks.
JsonOptions
Configuration for JSON normalization via jsonSafe and related helpers.
NumberOptions
Options that control numeric parsing.
TypeRegistry
Registry of custom parsers for application-specific types.
UriOptions
Options that control URI parsing and coercion.

Enums

DateTimeStrategy
Strategy for encoding DateTime values to JSON.
DurationStrategy
Strategy for encoding Duration values to JSON.
NonFiniteDoubleStrategy
Strategy for handling non-finite doubles (NaN, Infinity, -Infinity) in JSON.

Extensions

BoolParsingX on Object?
Adds boolean parsing helpers to dynamic values.
ConvertObjectExtension on Object?
Adds a convert getter for fluent type conversions on any value.
DateParsingTextX on String
Extension methods for parsing String values into DateTime instances.
EnumValuesParsing on List<T>
Convenience accessors for creating enum parsers directly from Enum.values.
IterableConversionX on Iterable<E>
Type conversion helpers for non-null Iterable collections.
JsonAnyX on Object?
JsonIterableX on Iterable<T>
JsonMapX on Map<K, V>
LetExtension on T
Kotlin-style scope functions for non-nullable types.
LetExtensionNullable on T?
Kotlin-style scope functions for nullable types.
MapConversionX on Map<K, V>
Type conversion helpers for non-nullable Map instances.
NullableIterableConversionX on Iterable<E>?
Conversion helpers for nullable Iterable collections.
NullableMapConversionX on Map<K, V>?
Conversion helpers for nullable maps.
NumParsingTextX on String
Extension methods for parsing numeric strings with lenient formatting.
RomanNumeralIntX on num
Adds Roman numeral conversion to numeric types.
RomanNumeralNullableStringX on String?
Adds nullable-safe Roman numeral parsing to strings.
RomanNumeralStringX on String
Adds Roman numeral parsing to strings.
SetConvertToX on Set<E>?
Converts nullable sets into a Set of a different type.
TextJsonX on String
Adds JSON decoding helpers to String.
UriParsingX on String
Extension methods for validating and converting URI-like strings.

Constants

romanNumerals → const Map<int, String>
Lookup table mapping integers to their Roman numeral representations.

Functions

convertToBigInt(dynamic object, {Object? mapKey, int? listIndex, BigInt? defaultValue, BigInt converter(Object?)?}) BigInt
Top-level convenience alias for Convert.toBigInt.
convertToBool(dynamic object, {Object? mapKey, int? listIndex, bool? defaultValue, bool converter(Object?)?}) bool
Top-level convenience alias for Convert.toBool.
convertToDateTime(dynamic object, {Object? mapKey, int? listIndex, String? format, String? locale, bool autoDetectFormat = false, bool useCurrentLocale = false, bool utc = false, DateTime? defaultValue, DateTime converter(Object?)?}) DateTime
Top-level convenience alias for Convert.toDateTime.
convertToDouble(dynamic object, {Object? mapKey, int? listIndex, String? format, String? locale, double? defaultValue, double converter(Object?)?}) double
Top-level convenience alias for Convert.toDouble.
convertToEnum<T extends Enum>(dynamic object, {required T parser(dynamic), Object? mapKey, int? listIndex, T? defaultValue, Map<String, dynamic>? debugInfo}) → T
Top-level convenience alias for Convert.toEnum.
convertToInt(dynamic object, {Object? mapKey, int? listIndex, String? format, String? locale, int? defaultValue, int converter(Object?)?}) int
Top-level convenience alias for Convert.toInt.
convertToList<T>(dynamic object, {Object? mapKey, int? listIndex, List<T>? defaultValue, T elementConverter(Object?)?}) List<T>
Top-level convenience alias for Convert.toList.
convertToMap<K, V>(dynamic object, {Object? mapKey, int? listIndex, Map<K, V>? defaultValue, K keyConverter(Object?)?, V valueConverter(Object?)?}) Map<K, V>
Top-level convenience alias for Convert.toMap.
convertToNum(dynamic object, {Object? mapKey, int? listIndex, String? format, String? locale, num? defaultValue, num converter(Object?)?}) num
Top-level convenience alias for Convert.toNum.
convertToSet<T>(dynamic object, {Object? mapKey, int? listIndex, Set<T>? defaultValue, T elementConverter(Object?)?}) Set<T>
Top-level convenience alias for Convert.toSet.
convertToString(dynamic object, {Object? mapKey, int? listIndex, String? defaultValue, String converter(Object?)?}) String
Top-level convenience alias for Convert.string.
convertToType<T>(dynamic object) → T
Top-level convenience alias for Convert.toType.
convertToUri(dynamic object, {Object? mapKey, int? listIndex, Uri? defaultValue, Uri converter(Object?)?}) Uri
Top-level convenience alias for Convert.toUri.
intToRomanNumeral(int value) String
Converts an integer to its Roman numeral representation.
jsonSafe(dynamic value, {JsonOptions options = const JsonOptions(), Object? toEncodable(dynamic object)?}) → dynamic
Returns a JSON-encodable form of value, honoring options.
romanNumeralToInt(String romanNumeral) int
Parses a Roman numeral string into its integer value.
tryConvertToBigInt(dynamic object, {Object? mapKey, int? listIndex, BigInt? defaultValue, BigInt converter(Object?)?}) BigInt?
Top-level convenience alias for Convert.tryToBigInt.
tryConvertToBool(dynamic object, {Object? mapKey, int? listIndex, bool? defaultValue, bool converter(Object?)?}) bool?
Top-level convenience alias for Convert.tryToBool.
tryConvertToDateTime(dynamic object, {Object? mapKey, int? listIndex, String? format, String? locale, bool autoDetectFormat = false, bool useCurrentLocale = false, bool utc = false, DateTime? defaultValue, DateTime converter(Object?)?}) DateTime?
Top-level convenience alias for Convert.tryToDateTime.
tryConvertToDouble(dynamic object, {Object? mapKey, int? listIndex, String? format, String? locale, double? defaultValue, double converter(Object?)?}) double?
Top-level convenience alias for Convert.tryToDouble.
tryConvertToEnum<T extends Enum>(dynamic object, {required T parser(dynamic), Object? mapKey, int? listIndex, T? defaultValue, Map<String, dynamic>? debugInfo}) → T?
Top-level convenience alias for Convert.tryToEnum.
tryConvertToInt(dynamic object, {Object? mapKey, int? listIndex, String? format, String? locale, int? defaultValue, int converter(Object?)?}) int?
Top-level convenience alias for Convert.tryToInt.
tryConvertToList<T>(dynamic object, {Object? mapKey, int? listIndex, List<T>? defaultValue, T elementConverter(Object?)?}) List<T>?
Top-level convenience alias for Convert.tryToList.
tryConvertToMap<K, V>(dynamic object, {Object? mapKey, int? listIndex, Map<K, V>? defaultValue, K keyConverter(Object?)?, V valueConverter(Object?)?}) Map<K, V>?
Top-level convenience alias for Convert.tryToMap.
tryConvertToNum(dynamic object, {Object? mapKey, int? listIndex, String? format, String? locale, num? defaultValue, num converter(Object?)?}) num?
Top-level convenience alias for Convert.tryToNum.
tryConvertToSet<T>(dynamic object, {Object? mapKey, int? listIndex, Set<T>? defaultValue, T elementConverter(Object?)?}) Set<T>?
Top-level convenience alias for Convert.tryToSet.
tryConvertToString(dynamic object, {Object? mapKey, int? listIndex, String? defaultValue, String converter(Object?)?}) String?
Top-level convenience alias for Convert.tryToString.
tryConvertToType<T>(dynamic object) → T?
Top-level convenience alias for Convert.tryToType.
tryConvertToUri(dynamic object, {Object? mapKey, int? listIndex, Uri? defaultValue, Uri converter(Object?)?}) Uri?
Top-level convenience alias for Convert.tryToUri.

Typedefs

DynamicConverter<T> = T Function(Object? value)
Signature for lazily transforming a stored value before conversion.
ElementConverter<T> = T Function(Object? element)
Signature for transforming a single element while converting collections.

Exceptions / Errors

ConversionException
Exception thrown when a type conversion fails.