首页 > 解决方案 > 颤振: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

如果您对问题出在哪里以及如何解决它有任何想法,请告诉我。谢谢

标签: flutterdart

解决方案


当您传递0_random.nextInt().

所以显然你的列表是空的并且widget.persons.length正在返回0

当您将其传递给小部件时,检查您的人员列表发生了什么。


推荐阅读