toSet<T> function

Set<T> toSet<T>(
  1. dynamic object, {
  2. Object? mapKey,
  3. int? listIndex,
})

Converts an object to a Set of type T. 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, throws a ParsingException with a nullObject error.
  • If the object cannot be converted to a Set of type T, throws a ParsingException.

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. Throws a ParsingException if the conversion fails.

Example usage:

final object1 = {1, 2, 3};
final set1 = toSet<int>(object1); // {1, 2, 3}

final object2 = [1, 2, 3];
final set2 = toSet<int>(object2); // {1, 2, 3}

final object3 = 'Hello';
final set3 = toSet<int>(object3); // ParsingException (logs an error)

final object4 = null;
final set4 = toSet<int>(object4); // ParsingException (null object)

Implementation

Set<T> toSet<T>(
  dynamic object, {
  Object? mapKey,
  int? listIndex,
}) =>
    ConvertObject.toSet(object, mapKey: mapKey, listIndex: listIndex);