FormatHelper extension

A set of extension methods for dynamic types to facilitate type conversion and provide default values when conversion is not possible. These methods help in safely converting dynamic values to specific types such as int, double, String, bool, List, ObjectId, and DateTime. Each method allows specifying a default value to return if the conversion fails. Example usage:

dynamic value = "123";
int intValue = value.asInt(def: 0); // Converts to int, returns 123
String strValue = value.asString(def: "default"); // Converts to String, returns "123"
bool boolValue = value.asBool(def: false); // Converts to bool, returns false
List listValue = value.asList(def: []); // Converts to List, returns []
ObjectId oidValue = value.asObjectId(def: ObjectId()); // Converts to ObjectId, returns a new ObjectId
DateTime dateTimeValue = value.asDateTime(def: DateTime.now()); // Converts to DateTime, returns current date and time
on
  • dynamic

Methods

asBool({bool? def}) bool

Available on dynamic, provided by the FormatHelper extension

Converts the dynamic value to a boolean. If the value is null, returns the provided default value or false. The conversion considers '1' and 'true' (case insensitive) as true, and any other value as false. def The default value to return if the dynamic value is null. Returns the boolean representation of the dynamic value or the default value.
asCast<T>({T? def}) → T?

Available on dynamic, provided by the FormatHelper extension

Attempts to cast the dynamic value to a specified type T. If the value is null or cannot be cast to T, returns the provided default value or null. Supports basic types such as int, double, num, String, bool, List, ObjectId, and DateTime. def The default value to return if casting fails. Returns the value cast to type T or the default value.
asDateTime({DateTime? def}) DateTime?

Available on dynamic, provided by the FormatHelper extension

Converts the dynamic value to a DateTime object. If the value is null, returns the provided default DateTime or a DateTime set to UTC 1977. If the value is a String, it attempts to parse it into a DateTime. If parsing fails, returns null. def The default DateTime to return if the value is null. Returns the DateTime representation of the dynamic value or the default DateTime.
asDouble({double? def}) double

Available on dynamic, provided by the FormatHelper extension

Converts the dynamic value to a double. If conversion fails, returns the provided default value or 0.0. def The default value to return if conversion fails. Returns the double representation of the dynamic value or the default value.
asInt({int? def}) int

Available on dynamic, provided by the FormatHelper extension

Converts the dynamic value to an integer. If conversion fails, returns the provided default value or 0. def The default value to return if conversion fails. Returns the integer representation of the dynamic value or the default value.
asList<T>({List<T>? def}) List<T>

Available on dynamic, provided by the FormatHelper extension

asNum({num? def}) num

Available on dynamic, provided by the FormatHelper extension

Converts the dynamic value to a num (either int or double). If conversion fails, returns the provided default value or 0. def The default value to return if conversion fails. Returns the num representation of the dynamic value or the default value.
asObjectId({ObjectId? def}) ObjectId

Available on dynamic, provided by the FormatHelper extension

Converts the dynamic value to a MongoDB ObjectId. If the value is already an ObjectId, it is returned as is. If the value is a String, it attempts to parse it into an ObjectId. If parsing fails or the value is null, returns the provided default ObjectId or a new ObjectId. def The default ObjectId to return if conversion fails. Returns the ObjectId representation of the dynamic value or the default ObjectId.
asString({String? def, bool trim = true}) String

Available on dynamic, provided by the FormatHelper extension

Converts the dynamic value to a String. If the value is null or empty after trimming, returns the provided default value or an empty string. def The default value to return if the dynamic value is null or empty. trim Whether to trim whitespace from the string (default is true). Returns the String representation of the dynamic value or the default value.