我正在尝试制作一个随机选择某人的应用程序。在页面中,有一些文本字段和一个按钮,您可以在其中输入您的姓名。当我单击该按钮时,它会将我带到结果页面,我想在其中显示随机选择的人的姓名。我在结果页面的构造函数中传递了人员列表,并做了以下操作:
import 'package:flutter/material.dart';
import 'dart:math';
class ResultScreen extends StatefulWidget {
final List persons;
ResultScreen({this.persons});
@override
_ResultScreenState createState() => _ResultScreenState();
}
class _ResultScreenState extends State<ResultScreen> {
final _random = Random();
// var selectedPerson = persons[_random.nextInt(persons.length)];
// print(selectedPerson);
@override
Widget build(BuildContext context) {
return Material(
child: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("lib/images/fond.jpg"), fit: BoxFit.cover)),
child: Center(
child: Text(' And the loser is\n ${widget.persons[_random.nextInt(widget.persons.length)]} ',
style: TextStyle(color: Colors.black, fontSize: 50, fontWeight: FontWeight.bold),),
),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
当我导航器推送结果页面时出现此错误:
RangeError (max): Must be positive and <= 2^32: Not in inclusive range 1..4294967296: 0
Run Code Online (Sandbox Code Playgroud)
如果您知道问题是什么以及如何解决它,请告诉我。谢谢
0当您传递到时会导致此错误_random.nextInt()。
所以显然你的列表是空的并且widget.persons.length正在返回0。
当您将人员列表传递给小部件时,请检查它发生了什么。
| 归档时间: |
|
| 查看次数: |
2746 次 |
| 最近记录: |