tryToString function
Converts any object to a string, or returns null if the object is null.
mirroring the same static method in the ConvertObject, providing alternative easy less code usage options.
- Converts various object types to their string representation.
- If the conversion to string fails, logs an error and returns
null.
object The object to be converted to a string.
mapKey (Optional) Specifies the key to extract values from a Map object.
listIndex (Optional) Specifies the index to extract elements from a List object.
Returns a string if conversion is successful, otherwise null.
Example usage:
final object1 = 'Hello';
final string1 = tryToString(object1); // 'Hello'
final object2 = 10;
final string2 = tryToString(object2); // '10'
final object3 = true;
final string3 = tryToString(object3); // 'true'
final object4 = null;
final string4 = tryToString(object4); // null
Implementation
String? tryToString(
dynamic object, {
Object? mapKey,
int? listIndex,
String? defaultValue,
}) =>
ConvertObject.tryToString(
object,
mapKey: mapKey,
listIndex: listIndex,
defaultValue: defaultValue,
);