randomElement method
Returns a random element from this iterable, or null if it is empty.
Pass seed for a deterministic pick: the same seed over the same
iterable always returns the same element, which is what reproducible demo
data and stable widget tests need. Omitting seed gives a fresh pick each
run (seeded from the wall clock via CommonRandom).
Picks by index via elementAt, so the iterable is not materialized to a list — only the chosen element is realized. Audited: 2026-06-12 11:26 EDT
Implementation
@useResult
T? randomElement({int? seed}) {
if (isEmpty) {
return null;
}
return elementAt(CommonRandom(seed).nextInt(length));
}