How to Shuffle Array List in Dart/Flutter

In this example, we are going to show you how to shuffle array List in Dart or Flutter. In Dart, Array is called List. The array is an important part of programming in any programming language. See the example below to shuffle array List in Dart.

Read this Also: List Basics in Dart/Flutter

Array List in Flutter:
List<String> countries = ["USA", "United Kingdom","China", "Russia", "Brazil"];
countries.shuffle();
print(countries); 
// [United Kingdom, Brazil, Russia, China, USA]
//the output will be in different order every time this code is executed.

List.shuffle() method is used to shuffle List in Dart. You can use this method in Flutter to shuffle a simple List or List of objects.  This is the recommended and most easiest way to shuffle array List in Flutter. Alternatively, you can create your own method to shuffle List in Dart.

import 'dart:math';
List shuffle(List array) {
    var random = Random(); //import 'dart:math';

    // Go through all elementsof list
    for (var i = array.length - 1; i > 0; i--) {

      // Pick a random number according to the lenght of list
      var n = random.nextInt(i + 1);
      var temp = array[i];
      array[i] = array[n];
      array[n] = temp;
    }
    return array;
} 
List<String> countries = ["USA", "United Kingdom","China", "Russia", "Brazil"];

List newlist = shuffle(countries);
print(newlist); //[Russia, China, USA, United Kingdom, Brazil]

Here, we have created custom function shuffle() to shuffle List. You can declare this method locally or globally and use it in Flutter/Dart.

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
     return MaterialApp(
         home: Home()
      );
  }
}

class Home extends  StatefulWidget {
  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {

    List<String> countries = ["USA", "United Kingdom","China", "Russia", "Brazil"];
    countries.shuffle();
    print(countries); 
    // [United Kingdom, Brazil, Russia, China, USA]
    //the output will be in different order every time this code is executed.

    return Scaffold(
         appBar: AppBar(
            title: Text("Shuffle List in Flutter"),
            backgroundColor: Colors.redAccent
         ),
          body: Container(
            alignment: Alignment.center,
            padding: EdgeInsets.all(20),
             child: Column(
                children: countries.map((country){
                    return Card( 
                      child:ListTile(
                          title: Text(country)
                      )
                    );
                }).toList(),
              )
          )
    );
  } 
}

In this way, you can shuffle Array List in Dart/Flutter. Use this method in your project and shuffle your array to create new array in random order.

No any Comments on this Article


Please Wait...