safe_converter library
A utility library for safe dynamic type conversion in Flutter/Dart.
This library provides a set of functions and extensions to safely convert
dynamic data types to expected types (e.g., int, double, String, bool, List, Map, DateTime).
It ensures values are correctly parsed, provides default values when necessary,
and handles edge cases like null, NaN, or Infinity.
Example:
final map = {"id": "123", "price": "99.9", "active": "yes"};
int id = map.getInt("id"); // 123
double price = map.getDouble("price"); // 99.9
bool active = map.getBool("active"); // true
Classes
-
ObjCodec<
S> -
A codec for custom object conversion.
encode converts an object of type
Sto a Map. decode converts a Map back to an object of typeS. - SafeConvertConfig
- Global configuration for SafeConverter.
Extensions
-
NonNulls4MapExtensions
on Map<
K?, V?> - Extension on Map providing utility to remove null keys or values.
- SafeConvertOnMap2Bool on Map?
- Extension on Map? to provide safe conversion of values to bool.
- SafeConvertOnMap2DateTime on Map?
- Extension on Map? to provide safe conversion of values to DateTime.
- SafeConvertOnMap2Double on Map?
- Extension on Map? to provide safe conversion of values to double.
- SafeConvertOnMap2Int on Map?
- Extension on Map? to provide safe conversion of values to int.
- SafeConvertOnMap2List on Map?
- Extension on Map? to provide safe conversion of values to List.
- SafeConvertOnMap2Map on Map?
- Extension on Map? to provide safe conversion of values to Map.
- SafeConvertOnMap2Num on Map?
- Extension on Map? to provide safe conversion of values to num.
- SafeConvertOnMap2String on Map?
- Extension on Map? to provide safe conversion of values to String.
- SafeConvertOnMap2T on Map?
- Extension on Map? to provide generic safe conversion of values.
- SafeConvertOnObject on Object
- Extension for general object conversion.
- SafeConvertOnObject2Bool on Object
- Extension on Object to provide safe conversion to bool.
- SafeConvertOnObject2DateTime on Object
- Extension on Object to provide safe conversion to DateTime.
- SafeConvertOnObject2Double on Object
- Extension on Object to provide safe conversion to double.
- SafeConvertOnObject2Int on Object
- Extension on Object to provide safe conversion to int.
- SafeConvertOnObject2List on Object
- Extension on Object to provide safe conversion to List.
- SafeConvertOnObject2Map on Object
- Extension on Object to provide safe conversion to Map.
- SafeConvertOnObject2Num on Object
- Extension on Object to provide safe conversion to num.
- SafeConvertOnObject2String on Object
- Extension on Object to provide safe conversion to String.
- SafeConvertOnObjectNullable on T?
- Extension for nullable objects providing safe non-null operations.
Functions
-
asBool(
Map? json, String key, {bool defaultValue = false}) → bool - Safely gets and converts Map value to bool.
-
asBoolOrNull(
Map? json, String key, {bool? defaultValue}) → bool? - Safely gets and converts Map value to bool?.
-
asDateTime(
Map? json, String key, {DateTime? defaultValue}) → DateTime - Safely gets and converts Map value to DateTime.
-
asDateTimeOrNull(
Map? json, String key, {DateTime? defaultValue}) → DateTime? - Safely gets and converts Map value to DateTime?.
-
asDouble(
Map? json, String key, {double defaultValue = 0.0}) → double - Safely gets and converts Map value to double.
-
asDoubleOrNull(
Map? json, String key, {double? defaultValue}) → double? - Safely gets and converts Map value to double?.
-
asInt(
Map? json, String key, {int defaultValue = 0}) → int - Safely gets and converts Map value to int.
-
asIntOrNull(
Map? json, String key, {int? defaultValue}) → int? - Safely gets and converts Map value to int?.
-
asList<
T> (Map? json, String key, {List< T> ? defaultValue}) → List<T> - Safely gets and converts Map value to List.
-
asListOrNull<
T> (Map? json, String key, {List< T> ? defaultValue}) → List<T> ? - Safely gets and converts Map value to List?.
-
asMap<
K, V> (Map? json, String key, {Map< K, V> ? defaultValue}) → Map<K, V> - Safely gets and converts Map value to Map.
-
asMapOrNull<
K, V> (Map? json, String key, {Map< K, V> ? defaultValue}) → Map<K, V> ? - Safely gets and converts Map value to Map?.
-
asNum(
Map? json, String key, {num defaultValue = 0}) → num - Safely gets and converts Map value to num.
-
asNumOrNull(
Map? json, String key, {num? defaultValue}) → num? - Safely gets and converts Map value to num?.
-
asString(
Map? json, String key, {String defaultValue = ""}) → String - Safely gets and converts Map value to String.
-
asStringOrNull(
Map? json, String key, {String? defaultValue}) → String? - Safely gets and converts Map value to String?.
-
asT<
T> (dynamic source, {required T defaultValue}) → T - Alias for tConvert.
-
asTOrNull<
T> (dynamic source) → T? -
Alias for
_tNullableConvert. -
boolConvert(
dynamic source, {bool defaultValue = false}) → bool - Converts dynamic value to bool with a default value.
-
boolNullableConvert(
dynamic source) → bool? - Converts dynamic value to bool?. Supports true/false, 1/0, yes/no, on/off.
-
dateTimeConvert(
dynamic source, {DateTime? defaultValue}) → DateTime - Converts dynamic value to DateTime with a default value.
-
dateTimeNullableConvert(
dynamic source) → DateTime? - Converts dynamic value to DateTime?.
-
doubleConvert(
dynamic source, {double defaultValue = 0.0}) → double - Converts dynamic value to double with a default value.
-
doubleNullableConvert(
dynamic source) → double? - Converts dynamic value to double?.
-
intConvert(
dynamic source, {int defaultValue = 0}) → int - Converts dynamic value to int with a default value.
-
intNullableConvert(
dynamic source) → int? - Converts dynamic value to int?.
-
listConvert<
V> (dynamic source, {List< V> ? defaultValue}) → List<V> - Converts dynamic value to List
-
listNullableConvert<
V> (dynamic source) → List< V> ? - Converts dynamic value to List
-
mapConvert<
K, V> (dynamic source, {Map< K, V> ? defaultValue}) → Map<K, V> - Converts dynamic value to Map<K, V> with a default value.
-
mapNullableConvert<
K, V> (dynamic source) → Map< K, V> ? - Converts dynamic value to Map<K, V>?. Filters out entries that cannot be converted to type K or V. Supports JSON string input.
-
numConvert(
dynamic source, {num defaultValue = 0}) → num - Converts dynamic value to num with a default value.
-
numNullableConvert(
dynamic source) → num? - Converts dynamic value to num?.
-
registerCodec<
T> (ObjCodec< T> objCodec) → void - Registers a custom codec for type T.
-
stringConvert(
dynamic source, {String defaultValue = '', bool useToString = true}) → String - Converts dynamic value to String with a default value.
-
stringNullableConvert(
dynamic source, {bool useToString = false}) → String? - Converts dynamic value to String?.
-
tConvert<
T> (dynamic source, {required T defaultValue}) → T - Top-level function to convert dynamic value to non-nullable type T with a default value.
-
tNullableConvert<
T> (dynamic source) → T? - Top-level function to convert dynamic value to nullable type T.
-
tryBool(
Map? json, String key, {bool? defaultValue}) → bool? - Alias for asBoolOrNull.
-
tryBoolConvert(
dynamic source) → bool? - Alias for boolNullableConvert.
-
tryDateTime(
Map? json, String key, {DateTime? defaultValue}) → DateTime? - Alias for asDateTimeOrNull.
-
tryDateTimeConvert(
dynamic source) → DateTime? - Alias for dateTimeNullableConvert.
-
tryDouble(
Map? json, String key, {double? defaultValue}) → double? - Alias for asDoubleOrNull.
-
tryDoubleConvert(
dynamic source) → double? - Alias for doubleNullableConvert.
-
tryInt(
Map? json, String key, {int? defaultValue}) → int? - Alias for asIntOrNull.
-
tryIntConvert(
dynamic source) → int? - Alias for intNullableConvert.
-
tryList<
T> (Map? json, String key, {List< T> ? defaultValue}) → List<T> ? - Alias for asListOrNull.
-
tryListConvert<
V> (dynamic source) → List< V> ? - Alias for listNullableConvert.
-
tryMap<
K, V> (Map? json, String key, {Map< K, V> ? defaultValue}) → Map<K, V> ? - Alias for asMapOrNull.
-
tryMapConvert<
K, V> (dynamic source) → Map< K, V> ? - Alias for mapNullableConvert.
-
tryNum(
Map? json, String key, {num? defaultValue}) → num? - Alias for asNumOrNull.
-
tryNumConvert(
dynamic source) → num? - Alias for numNullableConvert.
-
tryString(
Map? json, String key, {String? defaultValue}) → String? - Alias for asStringOrNull.
-
tryStringConvert(
dynamic source, {bool useToString = false}) → String? - Alias for stringNullableConvert.
-
tryT<
T> (dynamic source) → T? -
Alias for
_tNullableConvert. -
tryTConvert<
T> (dynamic source) → T? - Alias for tNullableConvert.