toList method Null safety

List<int> toList(
  1. [int? exists]
)

Lists all entries of this sequence.

You must specify the number of existing messages with the exists parameter, in case this sequence contains the last element '' in some form. Use the containsLast method to determine if this sequence contains the last element ''.

Implementation

List<int> toList([int? exists]) {
  if (exists == null && containsLast()) {
    throw StateError(
        'Unable to list sequence when * is part of the list and the \'exists\' parameter is not specified.');
  }
  if (_isNilSequence) {
    throw StateError('Unable to list non existent sequence.');
  }
  final idset = LinkedHashSet<int>.identity();
  if (_isAllAdded) {
    for (var i = 1; i <= exists!; i++) {
      idset.add(i);
    }
  } else {
    var index = 0;
    var zeroloc = _ids.indexOf(RANGESTAR, index);
    while (zeroloc > 0) {
      idset.addAll(_ids.sublist(index, zeroloc));
      // Using a for-loop because we must generate a sequence when reaching the "STAR" value
      idset.addAll([for (var x = idset.last + 1; x <= exists!; x++) x]);
      index = zeroloc + 1;
      zeroloc = _ids.indexOf(RANGESTAR, index);
    }
    if (index >= 0 && zeroloc == -1) {
      idset.addAll(_ids.sublist(index));
    }
  }
  if (idset.remove(STAR) && exists != null) {
    idset.add(exists);
  }
  return idset.toList();
}