stringToIterable<T> function Null safety

Iterable<T> stringToIterable<T>(
  1. String value,
  2. T toElement(
    1. String e
    )
)

Generates an Iterable of a specified conversion way by toElement from a String with a json array structure

Example:

String array = '[1,2]'
List<int> list = stringToIterable(array, (element) => int.parse(element)).toList();

Implementation

Iterable<T> stringToIterable<T>(
  final String value,
  final T Function(String e) toElement,
) {
  return value
      .replaceAll("[", "")
      .replaceAll("]", "")
      .split(",")
      .map<T>(toElement);
}