convertJSObjectToList<T> static method

List<T> convertJSObjectToList<T>(
  1. Object list, [
  2. T converter(
    1. Object
    )?
])

Convert a js list that is a basic js object to a List with type T. list is the js object to convert to a list. All the entries in the original list will be copied over.

converter is how to convert the entries in the list from their js types to something that dart can handle. By default it will use the castConverter which will just cast the object to T. If something else is required then you will need to provide your own method.

Implementation

static List<T> convertJSObjectToList<T>(final Object list,
    [final T Function(Object)? converter]) {
  // Test if the input is an iterator.
  if (_JSUtil.hasProperty(list, "next")) {
    var result = _JSUtil.callMethod(list, "next", []);
    final List<T> output = [];
    while (!_JSUtil.getProperty(result, "done")) {
      final value = _JSUtil.getProperty(result, "value");
      output.add(converter != null
          ? converter.call(value)
          : castConverter<T>(value));
      if (!_JSUtil.hasProperty(result, "next")) {
        break;
      }
      result = _JSUtil.callMethod(result, "next", []);
    }
    return output;
  } else {
    // Assume it is a normal JS array otherwise
    final length = _JSUtil.getProperty(list, "length") as int? ?? 0;
    final List<T> output = List.generate(
        length,
        (final index) => converter != null
            ? converter.call(_JSUtil.callMethod(list, "at", [index]))
            : castConverter<T>(_JSUtil.callMethod(list, "at", [index])));
    return output;
  }
}