List Methods in Flutter
a. shuffle() :
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.
List myList = [1,2,3];
myList.shuffle();
print(myList); // [3,1,2]
b. take() :
This method returns iterable starting from index 0 till the count provided from given list.
List myList = ['Cricket','Football','Hockey']
print(myList.take(2)); // (Cricket, Football)
c. add() :
Adds one element, passed in as an argument, to the end of a list.
List myList = [1,2,3];
myList.add(4);
print(myList); // [1,2,3,4]
d. addAll() :
Adds all the elements of one list to the end of the target list, maintaining ordering.
List myList = [1,2,3];
myList.addAll([4,5,]);
print(myList); // [1,2,3,4,5]
e. replaceRange() :
This method helps to replace/update some elements of the given list with the new ones. The start and end range need to be provided alongwith the value to be updated in that range.
List myList = [0,1,2,3,4,5,6];
myList.replaceRange(2,3,[10]);
print(myList); //[0,1,10,3,4,5,6]
f. skip() :
This method ignores the elements starting from index 0 till count and returns remaining iterable from given list.
List myList = ['Cricket','Football','Hockey'];
print(myList.skip(2)); //(Hockey)
g. getRange() :
This method returns elements from specified range [start] to [end] in same order as in the given list. Note that, start element is inclusive but end element is exclusive.
List myList = [1,2,3,4,5];
print(myList.getRange(1,4)); //(2,3,4)
h. whereType() :
This method returns iterable with all elements of specific data type.
var mixList = [1,"a",2,"b",3,"c",4,"d"] ;
var num = mixList.whereType<String>();
print(num); // (a,b,c,d)
i. asMap() :
This method returns a map representation of the given list. The key would be the indices and value would be the corresponding elements of the list.
List<String> sports = ['Cricket','Football','Tennis','Baseball'];
Map<int, String> map = sports.asMap();
print(map); // {0: Cricket, 1: Football, 2: Tennis, 3: Baseball }
j. reversed() :
Reversed is a getter which reverse iteration of the list depending upon given list order.
List myList = [1,2,3,4,5,6];
print(myList.reversed.toList()); // [6,5,4,3,2,1]