android - Flutter rangeError(index) 有效值范围为空

标签 android ios flutter mobile dart

我正在构建一个应用程序,它需要一个人员列表(来自另一个类“people1”)和一个 bool 列表(如果有人在这里或不在)(“peoplePres”)。所以我在一系列循环中运行程序,这是我想到的算法。我得到的错误是

rangeError (index) Valid value range is empty

我坐在这里大约 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;
    }
  • 如果您在初始化列表时知道列表的内容应该是什么,则可以使用基于集合的 for 循环来构建列表,类似于 Python 的列表理解:
  •     List<int> list1 = [for (int i = 0; i < people1.length; i++) i];
    

    关于android - Flutter rangeError(index) 有效值范围为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59533982/

    相关文章:

    android - Flutter - 无法在android设备中运行

    ios - 可以从 web URL 和本地目录按文件名加载图像的扩展

    ios - 如何在不发布App的情况下在App Store中打开我的App进行更新

    flutter - 点击被多次识别

    java - 如何让按钮适应 Android Studio 中的任何屏幕尺寸

    ios - 如何访问 "cell for row at indexpath"之外的 uitableview 单元格元素

    flutter - 如何修复 FlexibleSpaceBar (SliverAppBar) 中标题的 textSize?

    android - Intent.getExtras.getInt() 与intent.getIntExtra() 相同吗?

    android - 数据库中的数据太多

    android - 康威的生命游戏应用生命规则问题 Android