[英]Flutter: RangeError (index): Invalid value: Valid value range is empty: 0
[英]Flutter rangeError (index) Valid value range is empty
我正在構建一個應用程序,它需要一個人員列表(來自另一個類“people1”)和一個布爾列表(如果人們在這里或不在這里)(“peoplePres”)。 所以我在一系列循環中運行程序,這是我想到的算法。 我得到的錯誤是
rangeError (index) 有效值范圍為空
我在這里坐了大約 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
。
您有幾個選項可以解決此問題:
List<int> list1 = List(length);
for (int i = 0; i < people1.length; i++) {
list.add(i); // instead of list1[i] = i;
}
List<int> list1 = [for (int i = 0; i < people1.length; i++) i];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.