nextInt method

  1. @override
  2. @Possible({StateError, RangeError})
  3. @useResult
int nextInt(
  1. int max
)
override

Returns the next integer in the iterable.

Contract

Throws:

  • a StateError if there are no more integers in the iterable.
  • a RangeError if an integer in the iterable is outside the range, 0 <= integer < max.

Example

final random = FakeRandom(ints: [1, 2]);
random.nextInt(5); // 1
random.nextInt(5); // 2
random.nextInt(5); // throws StateError

Implementation

@override
@Possible({StateError, RangeError})
@useResult int nextInt(int max) {
  if (!_ints.moveNext()) {
    throw StateError('FakeRandom presently does not contain an integer. Try supply more integers to `FakeRandom(ints: [...])`.');
  }

  if (max <= 0) {
    throw RangeError('max is $max, should be in the range `0 < max`.');
  }

  if (_ints.current < 0 || max <= _ints.current) {
    throw RangeError('The current integer, ${_ints.current} is outside the range, `0 <= ${_ints.current} < $max`. Try change the integers supplied to `FakeRandom(ints: [...])`.');
  }

  return _ints.current;
}