randomOrNull method

T? randomOrNull([
  1. Random? random
])

Returns a random element from this collection.

returns null if this collection is empty.

Implementation

T? randomOrNull([math.Random? random]) {
  if (!isNotEmpty()) return null;
  final r = random ?? math.Random();
  final index = r.nextInt(size);
  if (index >= size) return null;
  return elementAt(index);
}