I am new to flutter. I have one String Array and it has 20 values. However, I want to take 12 random values from this array and create a new array with this values. Can you show me the way?
Adapted to this question
var list = ['a', 'b', 'c', 'd', 'e'];
var newList = [];
//adjust to you with 12
for (int i = 0; i < 3; i++) {
// generates a new Random object
final _random = new Random();
// generate a random index based on the list length
// and use it to retrieve the element
int index = _random.nextInt(list.length);
var element = list[index];
//add the element to your new list and remove it to the old list
newList.add(element);
list.removeAt(index);
}
print(newList);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.