簡體   English   中英

Flutter rangeError (index) 有效值范圍為空

[英]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

您有幾個選項可以解決此問題:

  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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM