list - 如何显示列表的随机项?[每 9 秒]
问题描述
有没有办法每 9 秒自动显示一个 randomQuote?
下面是重要的代码的截断部分:
final List<String> RandomQuotes = [ ... ] // (one hundreds quotes here)
var randomQuote = (RandomQuotes.toList()..shuffle()).first;
:
:
:
... Text(randomQuote, Style:...)
我已经尝试过您建议的代码,但没有使用 randomQuote 变量。如下图所示。
解决方案
很简单,只需使用 aTimer.periodic
并定义 9 秒的持续时间:
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);
});
推荐阅读
- c++ - CShellManager 初始化失败
- makefile - 使用 Makefile 将目标文件编译到发布和调试文件夹中
- firebase - firebase t.INTERNAL.registerService 不是部署时的函数(使用 vue)
- python - 为什么从 MATLAB 调用此 python 脚本时无法正常工作?
- flutter - Flutter的列表视图构建器中的“长列表”是什么意思
- php - 为什么 PHP fetch 不显示任何内容?
- memory - 为什么编译后会分配两块内存区域,是不是很浪费内存?
- html - 页脚没有响应应有的响应
- azure - 如何将 Orchard Core 从 localhost 部署到 Azure
- css - 边框在出现时正在移动 div