颤振范围错误(索引) 有效值范围为空



我正在构建一个应用程序,该应用程序需要人员列表(来自另一个类"people1"(和布尔列表(如果人们是否在这里(("peoplePres"(。所以我在一系列循环中运行程序,这是我想到的算法。我得到的错误是

范围

错误(索引(有效值范围为空

我坐在这里大约 9 个小时试图解决问题,我不知道你是否能提供帮助,那将是惊人的。 我也不确定我是否使用共享首选项包正确保存列表"lastPres"列表。 提前谢谢你。

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class Result extends StatelessWidget {
List<String> people1;
List<bool> peoplePres;
Result({this.people1, this.peoplePres});
List<String> lastPres = [];
void initState() {
_lastZakif();
}
void _lastZakif() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
lastPres = (prefs.getStringList('lastPres') ?? 0);
}
void _loadingNew() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
lastPres = people1;
await prefs.setStringList('lastPres', lastPres);
}
@override
Widget build(BuildContext context) {
int count = 0; //count for how many people are here
int count2 = 1; //count for the new order
List<int> list1 = []; //List with the algorithm
List<int> list2 = []; //List with the new order
for (int i = 0; i < people1.length; i++) {
//counting People that are here
if (peoplePres[i] == true) {
count++;
}
}
for (int i = 0; i < people1.length; i++) {
// Declaring a list which will be the index for the next Zkifut
list1[i] = i;
}
for (int i = 0; i < people1.length; i++) {
//Applying the algorithm
int num1 = count ~/ 3;
if (num1 % 3 == 2) {
num1 += 1;
}
list1[i] = list1[i] + num1 ~/ 3 - count;
}
for (int i = 0; i < people1.length; i++) {
// making the new schedule for absent people but starting with 0
if (peoplePres[i] == false) {
list2[i] = 0;
break;
}
}
for (int i = 0; i < people1.length; i++) {
// makeing the new schedule pos num
if ((list1[i] >= 0) && (peoplePres[i] == true)) {
list2[i] = count2;
count2++;
}
}
for (int i = 0; i < people1.length; i++) {
// makeing the new schedule neg num
if ((list1[i] < 0) && (peoplePres[i] == true)) {
list2[i] = count2;
count2++;
}
}
for (int i = 0; i < people1.length; i++) {
// makeing the new schedule for absent people
if ((peoplePres[i] == false) && (list2[i]) != 0) {
list2[i] = count2;
count2++;
}
}
for (int i = 0; i < people1.length; i++) {
people1[list2[i]] = people1[i];
}
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: Text(
'Result',
style: TextStyle(fontSize: 30),
),
),
body: ListView.builder(
itemCount: people1.length,
itemBuilder: (context, value) {
return Card(
color: Colors.amberAccent[200],
elevation: 3,
child: Container(
child: ListTile(
leading: Text('${value + 1}'),
title: Text(
people1[value],_loadingNew();
),
),
),
);
}),
);
}
}

您正在尝试使用带有空列表的operator[]=来设置值,这将导致抛出RangeError

例如,在生成方法的开头,创建两个列表:

List<int> list1 = []; //List with the algorithm
List<int> list2 = []; //List with the new order

然后在第二个循环中,您尝试分配给空列表中的索引:

for (int i = 0; i < people1.length; i++) {
// Declaring a list which will be the index for the next Zkifut
list1[i] = i; // This is where your first exception is coming from
}

您不能只写入 Dart 列表中的任意索引,因为您需要确保要写入的索引在 0 和list.length - 1的范围内。如果列表为空,则有效范围为 [0, 0],这意味着您将始终抛出RangeError

您有几个选项可以解决此问题:

  1. 如果您事先知道列表的长度,则可以将列表预先分配为特定大小,并将代码的其余部分保留原样:
List<int> list1 = List(length);
  1. 您可以使用空列表进行初始化,然后将每个元素添加到列表的末尾:
for (int i = 0; i < people1.length; i++) {
list.add(i); // instead of list1[i] = i;
}
  1. 如果你在初始化列表时知道列表的内容应该是什么,你可以使用基于集合的 for 循环来构建列表,类似于 Python 的列表推导
List<int> list1 = [for (int i = 0; i < people1.length; i++) i];

最新更新