How to get a random item from a collection in Dart?
var list = ['a','b','c','d','e'];
import "dart:math"; var list = ['a','b','c','d','e']; // 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 = list[_random.nextInt(list.length)];
This also works:
var list = ['a','b','c','d','e']; var randomItem = (list..shuffle()).first;
or, if you do not want to spoil the list, create a copy:
var randomItem = (list.toList()..shuffle()).first;
You can use the dart_random_choice package to help you.
import 'package:dart_random_choice/dart_random_choice.dart'; var list = ['a','b','c','d','e']; var el = randomChoice(list);
Source: https://habr.com/ru/post/1489784/More articles:Remove tags (and tag contents) but leave text - javascriptFilter RavenDB Static Map / Reduce Index - mapreduceSupport for WebGL extension in browsers - webglHow to create a custom Facebook sharing dialog? - facebookHow to properly create a Map / Reduce index for RavenDB in C # - c #MVC 4 Why do I have to serialize on the server, but not locally? - c #Complete list of advanced extensions in WebGL2 - webglWeb Services - what is it? - soapHow to set iOS navigationItem tooltip without animation? - iosA simple soap call does not work (Nodejs, easysoap) - soapAll Articles