Flutter rangeError(索引)有效值范围为空

时间:2019-12-30 16:33:27

标签: android ios flutter mobile dart

我正在构建一个应用程序,该应用程序包含一个人列表(来自另一个类“ people1”)和一个bool列表(无论是否有人)(“ peoplePres”)。因此,我在一系列循环中运行该程序,这是我想到的一种算法。我得到的错误是

  

rangeError(索引)有效值范围为空

我坐在这里约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();
                  ),
                ),
              ),
            );
          }),
    );
  }
}

1 个答案:

答案 0 :(得分:0)

您正尝试使用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];