random property
T
get
random
Returns a random element from the list.
If the list is not empty, this method selects a random index using Random().nextInt(length)
and returns the corresponding element.
Throws an exception if the list is empty.
Example:
List<String> greetings = ["Hello!", "Hi!", "Welcome!"];
String randomGreeting = greetings.random;
print(randomGreeting); // Output: A random greeting from the list
Implementation
T get random {
return isNotEmpty
? (this as List<T>)[Random().nextInt(length)]
: throw Exception("List is empty");
}