tryToSet<T> function
Attempts to convert an object to a Set of type T
, or returns null if conversion is not possible.
mirroring the same static method in the ConvertObject, providing alternative easy less code usage options.
- If the object is already a Set of type
T
, it is returned as-is. - If the object is an Iterable, it converts it to a Set of type
T
. - If the object is null, returns null.
- If the object cannot be converted to a Set of type
T
, logs an error and returns null.
object
The object to be converted to a Set of type T
.
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 Set of type T
if conversion is successful, otherwise null.
Example usage:
final object1 = {1, 2, 3};
final set1 = tryToSet<int>(object1); // {1, 2, 3}
final object2 = [1, 2, 3];
final set2 = tryToSet<int>(object2); // {1, 2, 3}
final object3 = 'Hello';
final set3 = tryToSet<int>(object3); // null (logs an error)
final object4 = null;
final set4 = tryToSet<int>(object4); // null
Implementation
Set<T>? tryToSet<T>(
dynamic object, {
Object? mapKey,
int? listIndex,
}) =>
ConvertObject.tryToSet(object, mapKey: mapKey, listIndex: listIndex);