【发布时间】:2020-04-19 09:15:16
【问题描述】:
我正在构建一个应用程序,它需要一个人员列表(来自另一个类“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();
),
),
),
);
}),
);
}
}
【问题讨论】:
-
错误在哪一行?
-
当我编译代码而不是任何特定行时会发生错误,这就是为什么你没有看到问题所在的行
标签: android ios flutter mobile dart