Ad
How To Display A Random Item Of A List? [Every 9 Seconds]
Is there a way to display a randomQuote every 9 seconds automatically?
Below is a truncated portion of the code that matters:
final List<String> RandomQuotes = [ ... ] // (one hundreds quotes here)
var randomQuote = (RandomQuotes.toList()..shuffle()).first;
:
:
:
... Text(randomQuote, Style:...)
I've tried your suggested code, but the randomQuote var is not being used. As seen in the images below.
Ad
Answer
Quite easy just use a Timer.periodic
and define a duration of 9 seconds:
import 'dart:async';
import 'dart:math';
final items = <String>['Hi', 'Hello', 'Test'];
Timer.periodic(Duration(seconds: 9), (_) {
final _random = Random();
final item = items[_random.nextInt(items.length)];
print(item);
});
Ad
source: stackoverflow.com
Related Questions
- → OctoberCMS - How to make collapsible list default to active only on non-mobile
- → Octobercms/Laravel get id based on relation
- → How do i get base url in OctoberCMS?
- → Eloquent Multitable query
- → ListView.DataSource looping data for React Native
- → Laravel File Listing & Counting by Filtered File Names
- → List class element(index).fadetoggle(some speed) not working
- → Using Array in '->where()' for Laravel Query Building
- → List of react-native StyleSheet properties and options
- → How to access a complex object comming from a datatable cell in JQuery/Javascript
- → getting the correct record in Angular with a json feed and passed data
- → CasperJS - NodeList.length return 0
- → Plugin with single item ( october cms )
Ad