flutter - 颤振:RangeError(最大值):必须为正且<= 2 ^ 32:不在包含范围1..4294967296:0
问题描述
我正在尝试做一个随机选择某人的应用程序。在页面中有一些文本字段,您可以在其中输入您的姓名和一个按钮。当我单击按钮时,它会将我带到结果页面,我想在其中显示随机选择的人的姓名。我在结果页面的构造函数中传递了人员列表并做了这个:
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),),
),
),
);
}
}
当我导航器推送结果页面时出现此错误:
RangeError (max): Must be positive and <= 2^32: Not in inclusive range 1..4294967296: 0
如果您对问题出在哪里以及如何解决它有任何想法,请告诉我。谢谢
解决方案
当您传递0
到_random.nextInt()
.
所以显然你的列表是空的并且widget.persons.length
正在返回0
。
当您将其传递给小部件时,检查您的人员列表发生了什么。
推荐阅读
- java - 将 "condition ? expr1 : expr2" 链转换为 "if" 链
- android - 无法在 Android 3.4 中同步 gradle 项目
- c# - 如何正确解析 ConfigureServices() 中的实例?
- machine-learning - 余弦相似度实际用例
- node.js - 如何捕获特定函数调用的标准输出?
- c# - 调用并将项目添加到 datagridview 中的新组合框中
- java - Spring 集成测试失败 - 缺少 Flyway 配置
- javascript - 使用 Prometheus 监控 React 前端应用程序
- php - php会话更改值
- python - 如果 Queue.get() 被中断,Python 线程锁永远不会被释放