In this tutorial, we’ll learn how to shuffle list in Flutter. We’ll be using multiple Flutter code examples to understand it practically.
After reading this post, you’ll have a detailed practical knowledge of how to properly shuffle list in Flutter.
So without any delay, let’s just throw ourselves right into its practical implementation.
Introduction: Shuffle List In Flutter
It specifies a process through which we can change the order of list items randomly. We have to use shuffle() method in order to perform this action.
Let’s now understand it practically using multiple code examples.
Syntax of Shuffle Method
listName.shuffle()
We’ve to use the shuffle() method like this.
Implement Shuffle List In Flutter (Easy Code Examples)
See below examples to understand the usage/role of shuffle() method in Flutter list.
Example 1: Shuffle List In Flutter
Let’s first create a list in Flutter. After that, we’ll apply shuffle() method on it. See below code:
List listOfValues=[1,2,4,3]; listOfValues.shuffle(); // shuffle the list print(listOfValues);
Output
[3, 4, 1, 2] // list items has been shuffled [3, 2, 1, 4] // running the code again will also shuffle the list again
You will get an updated shuffled list each time you run the code. You can apply shuffle method to list again and again by using this listName.shuffle() statement.
Example 2: shuffle() Method Updates the Existing List
This method does not return anything. It just update/shuffle the current list. See below code:
List listOfValues=['green',3.4,4,2,5,6,34.78]; listOfValues.shuffle(); // shuffles the existing list print(listOfValues);
Output
[34.78, 3.4, 2, green, 5, 6, 4]
So this is how we can properly shuffle list in Flutter. Hope you have learned alot from this Flutter tutorial. Do use this method in your own Flutter code and share your experience with us and also with other Flutter developers.
Do ask if you still have any questions regarding the implementation of Flutter list shuffle method. I’ll be very happy to answer all.
Conclusion
To conclude this post, hope you now have a proper and detailed practical knowledge of how to shuffle list in Flutter. I’ll be very glad to receive your feedback on this post.
I’d love to see you visit my other articles as well. Thank you for reading this post.