问题描述
我有清单:
List quotes = [
"Lmao this is text","Okay okay go to next","So,we are the champion nanana","Gagagagaga","What does the fox say?"
];
var _index = new Random();
我想从列表元素中创建随机文本生成器。 我在Flutter中使用statefull,当我点击按钮时,我希望从列表中添加新的随机元素。 我的代码示例:
children: [
Text(quotes[_index]),Center(
child: Container(
child: FlatButton.icon(
onpressed: _showFate(),icon: Icon(Icons.casino),label: Text("New words!",style: TextStyle(
color: Colors.white
),)),_showFate() {
setState(() {
_index.nextInt(5);
});
为什么不起作用,我不明白...
解决方法
unable to retrieve container logs for docker://9b38be8792e9d3f0ecbd49411c9cc1bb9a3c8fa523097c65d9a8f5eaa2dcd5c7
应该是_index
而不是int
,并且您还应该在Random
_index
检查一下。
setState
,
只需在文本窗口小部件内使用_index.nextInt(quotes.length)
并调用setState进行更新:
children: [
Text(quotes[_index.nextInt(quotes.length)]),Center(
child: Container(
child: FlatButton.icon(
onPressed: _showFate(),icon: Icon(Icons.casino),label: Text("New words!",style: TextStyle(
color: Colors.white
),)),_showFate() {
setState(() {});
}