【问题标题】:Flutter rangeError (index) Valid value range is emptyFlutter rangeError(index) 有效值范围为空
【发布时间】: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


【解决方案1】:

您正在尝试使用带有空列表的 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];

【讨论】:

    猜你喜欢
    • 2020-11-01
    • 2022-08-17
    • 2022-11-03
    • 2020-06-24
    • 2021-10-06
    • 2021-11-13
    • 1970-01-01
    • 2022-07-18
    • 2021-06-16
    相关资源
    最近更新 更多