Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

flutter – how to display a random name from a list of names

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading