toList<T> static method

List<T> toList<T>({
  1. List? list,
  2. String? value,
  3. String regex = ",",
})

Converts a comma-separated string or list to a list of a specified type.

Example: '1,2,3' -> 1, 2, 3

Implementation

static List<T> toList<T>({
  List<dynamic>? list,
  String? value,
  String regex = ",",
}) {
  if (list != null && list.isNotEmpty && list.first is T) {
    return list.cast<T>();
  } else if (value != null) {
    return value.split(regex).cast<T>();
  } else {
    return [];
  }
}