title: Flutter rangeError (index) Valid value range is empty
body: <p>I am buidling an app which takes a list of people (from another class 'people1') and a list of bool if people are here or not ("peoplePres"). So I run the program in a series of loops which is an algorithm I thought of. The error i got is </p>
  
  <blockquote>
    <p>rangeError (index) Valid value range is empty</p>
  </blockquote>
  
  <p>I am sitting here for about 9 hours trying to solve the problem and i dont know how if you can help that will be amazing.
  also I am not sure I am saving the List "lastPres" list correctly using shared preferences packages.
  Thank you in advance.  </p>
  
  <pre><code>import 'package:flutter/material.dart';
  import 'package:shared_preferences/shared_preferences.dart';
  
  class Result extends StatelessWidget {
    List&lt;String&gt; people1;
    List&lt;bool&gt; peoplePres;
    Result({this.people1, this.peoplePres});
    List&lt;String&gt; 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&lt;int&gt; list1 = []; //List with the algorithm
      List&lt;int&gt; list2 = []; //List with the new order
  
      for (int i = 0; i &lt; people1.length; i++) {
        //counting People that are here
        if (peoplePres[i] == true) {
          count++;
        }
      }
      for (int i = 0; i &lt; people1.length; i++) {
        // Declaring a list which will be the index for the next Zkifut
        list1[i] = i;
      }
      for (int i = 0; i &lt; 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 &lt; 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 &lt; people1.length; i++) {
        // makeing the new schedule pos num
        if ((list1[i] &gt;= 0) &amp;&amp; (peoplePres[i] == true)) {
          list2[i] = count2;
          count2++;
        }
      }
      for (int i = 0; i &lt; people1.length; i++) {
        // makeing the new schedule neg num
        if ((list1[i] &lt; 0) &amp;&amp; (peoplePres[i] == true)) {
          list2[i] = count2;
          count2++;
        }
      }
      for (int i = 0; i &lt; people1.length; i++) {
        // makeing the new schedule for absent people
        if ((peoplePres[i] == false) &amp;&amp; (list2[i]) != 0) {
          list2[i] = count2;
          count2++;
        }
      }
      for (int i = 0; i &lt; 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();
                    ),
                  ),
                ),
              );
            }),
      );
    }
  }
  </code></pre>