To shuffle a list in Dart, you can use the shuffle
method provided by the List class.
In this example,
numbers
with integer values.shuffle
method on the list object numbers
.shuffle
method randomly rearranges the elements in the list in place.void main() {
List<int> numbers = [1, 2, 3, 4, 5];
numbers.shuffle();
print('Shuffled list: \$numbers');
}
Shuffled list: [3, 1, 5, 4, 2]
In this example,
words
with string values.shuffle
method on the list object words
.shuffle
method randomly rearranges the elements in the list in place.void main() {
List<String> words = ['apple', 'banana', 'cherry', 'date', 'elderberry'];
words.shuffle();
print('Shuffled list: \$words');
}
Shuffled list: [cherry, elderberry, apple, banana, date]
In this tutorial, we learned How to Shuffle a List in Dart language with well detailed examples.