UniqueList<E>.generate constructor

UniqueList<E>.generate(
  1. int length,
  2. Generator<E> generator, {
  3. bool growable = true,
  4. bool strict = false,
  5. bool nullable = true,
})

Generates a list of values.

Creates a list with length positions and fills it with values created by calling generator for each index in the range 0 .. length - 1 in increasing order.

List<int>.generate(3, (int index) => index/// index); // [0, 1, 4]

The created list is fixed-length if growable is set to false.

If strict is true, a DuplicateValueError will be thrown when a value is added to the list that already exists in the list.

If nullable is true, the list may contain multiple instances of null, otherwise, null will be treated like any other value and only one instance of null may be contained in the list.

The length must be non-negative.

Implementation

factory UniqueList.generate(
  int length,
  Generator<E> generator, {
  bool growable = true,
  bool strict = false,
  bool nullable = true,
}) {
  assert(length >= 0);

  var list = List<E>.generate(length, generator, growable: growable);

  if (!strict && growable) {
    list = _removeDuplicateValues<E>(list, nullable: nullable);
  } else if (_containsDuplicateValues(list, nullable: nullable)) {
    throw DuplicateValuesError(
        UniqueList._getDuplicateValue<E>(list, nullable: nullable));
  }

  return UniqueList<E>._(list, nullable: nullable, strict: strict, growable: growable);
}