Please how do you display a random name from a list of names in flutter?
for example
List names = [‘jerry’,’mark’,’john’];
how do i display a random name?
>Solution :
please try this code:
import "dart:math";
List names = ['jerry','mark','john'];
// 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
var element = names[_random.nextInt(names.length-1)];
This method would also work:
var randomName = (names.toList()..shuffle()).first;
Credit to How do get a random element from a List in Dart?