Back

Shuffle a list in dart

void main() {
  var deck = Deck();
  print(deck);
  deck.shuffle();
  print(deck);
}

class Deck {
  List<Card> cards = [];

  Deck() {
    var ranks = [
      'Ace',
      'Two',
      'Three',
      'Four',
      'Five',
      'Six',
      'Seven',
      'Eight',
      'Nine',
      'Ten',
      'Jack',
      'Queen',
      'King'
    ];
    var suites = ['Diamonds', 'Hearts', 'Clubs', 'Spades'];

    for (var suit in suites) {
      for (var rank in ranks) {
        var card = Card(rank, suit);
        cards.add(card);
      }
    }
  }

  @override
  toString() {
    return cards.toString();
  }

  shuffle() {
    cards.shuffle();
  }
}

class Card {
  String rank;
  String suit;

  Card(this.rank, this.suit);

  @override
  toString() {
    return '$rank of $suit';
  }
}

Shuffle