shuffle method

  1. @override
void shuffle([
  1. Random? random
])
override

Shuffles the elements of this list randomly.

final numbers = <int>[1, 2, 3, 4, 5];
numbers.shuffle();
print(numbers); // [1, 3, 4, 5, 2] OR some other random result.

Implementation

@override
void shuffle([Random? random]) {
  _context.conditionallyRunInAction(() {
    if (_list.isNotEmpty) {
      final oldList = _list.toList(growable: false);
      _list.shuffle(random);
      final changes = <ElementChange<T>>[];
      for (var i = 0; i < _list.length; ++i) {
        final oldValue = oldList[i];
        final newValue = _list[i];
        if (newValue != oldValue) {
          changes.add(ElementChange(
              index: i, oldValue: oldValue, newValue: newValue));
        }
      }
      if (changes.isNotEmpty) {
        _notifyElementsUpdate(changes);
      }
    }
  }, _atom);
}