newListFrom method

List<T> newListFrom(
  1. Iterable<T> iterable, {
  2. bool growable = true,
  3. bool reactive = true,
})

A method similar to the list constructor List.from.

By default, the new list is growable. You can create a non-growable list by setting growable: false.

By default, the new list is wrapped with ReactiveList. You can disable this behavior by setting reactive: false.

The method allocates the most performant list possible. For instance, Float32Kind, will give you a dart:typed_data Float32List rather than normal List<double> (if growable and reactive are false).

Example

import 'package:kind/kind.dart';

void main() {
  final kind = StringKind();
  final list = kind.newListFrom(
    ['a', 'b', 'c'],
    growable: true,
    reactive: false,
  );
}

Implementation

List<T> newListFrom(Iterable<T> iterable,
    {bool growable = true, bool reactive = true}) {
  late List<T> oldList;
  if (iterable is List<T>) {
    if (iterable is ReactiveList<T>) {
      oldList = iterable.wrapped;
    } else {
      oldList = iterable;
    }
  } else {
    oldList = iterable.toList();
  }
  final length = oldList.length;
  final list = newList(length, growable: growable, reactive: false);
  for (var i = 0; i < length; i++) {
    list[i] = oldList[i];
  }
  if (reactive && (length != 0 || growable)) {
    return ReactiveList<T>.wrap(list);
  }
  return list;
}