Home > Back-end >  RangeError (index): Invalid value: Valid value range is empty: 0 when trying to check String
RangeError (index): Invalid value: Valid value range is empty: 0 when trying to check String

Time:10-28

Im trying to check users input from textFormField and check it with correct answer from list 'question_list' In this page user should input correct data to TextFormField. In for loop I'm trying to check user input from String list 'answer' and from list 'questionList'. for loop in setState() gives error

List question = questionList[0]['unit_1'];
return PageView.builder(
      itemBuilder: (BuildContext context, int index) {
        if (question[index]['type'] == 'text') {
          List correct = [];
          List<String> answer = [];
          for (var i = 0; i <= question[index]['correct_answer'].length; i  ) {
            correct.insert(i, 0);
          }
          return Scaffold(
            floatingActionButton: new FloatingActionButton(
                backgroundColor: Colors.green,
                elevation: 0,
                child: new Icon(
                  Icons.check,
                  size: 30,
                ),
                onPressed: () {
                  setState(() {
                    for (int i = 0;
                        i <= question[0]['correct_answer'].length;
                        i  ) {
                      if (question[0]['correct_answer'][i] == answer[i]) {
                        correct.insert(i, 1);
                      } else {
                        correct.insert(i, 2);
                      }
                    }
                  });
                }),

This is how I fill 'answer' list by TextField

                                  child: TextFormField(
                                    decoration: InputDecoration(
                                        border: OutlineInputBorder(),
                                        contentPadding: EdgeInsets.zero),
                                    onChanged: (value) {
                                      answer.insert(questionIndex.compareTo(0), value);
                                      print(value);
                                    },
                                    style: new TextStyle(
                                        fontWeight: FontWeight.normal,
                                        color: correct[questionIndex] == 2
                                            ? Colors.red
                                            : correct[questionIndex] == 1
                                                ? Colors.green
                                                : Colors.black),
                                  )),

This is list from which I get data

const List questionList = [
  {
    'unit_1': [
      {
        "id": 1,
        "type": "text",
        "question": [
          'Мен мұғалім',
          'Сен оқушы',
          'Сіз ана',
          'Сен спортшы',
          'Сіз әнші',
          'Мен қыз',
          'Сен Аружан',
          'Сіз дәрігер',
        ],
        "correct_answer": [
          'мін',
          'сын',
          'сыз',
          'сын',
          'сіз',
          'бын',
          'сын',
          'сіз'
        ],
      },
      {
        "id": 2,
        "type": "scroll",
        "question": [
          "Мен бала",
          "Сен әке",
          "Ол ұл",
          "Сіз дәрігер",
          "Сен спортшы",
          "Ол әнші",
          "Сіз ата",
          "Мен қыз",
          "Мен жігіт",
          "Мен ана",
          "Мен әже",
          "Мен Айдос",
          "Мен Ғазиз",
          "Мен мұғалім",
          "Сен аға"
        ],
        "correct_answer": [
          "",
          "мын",
          "мін",
          "бын",
          "бін",
          "пын",
          "пін",
          "сың",
          "сің",
          "сыз",
          "сіз",
        ],
      },

This causes error RangeError (index): Invalid value: Valid value range is empty: 0

CodePudding user response:

The problem is that you're trying to access an index that is greater than the list itself.

The problem is happening because you're using the list .length property. But the index of a list is zero-based (starts with zero)

Try replacing the <= for <:

for (int i = 0; i < question[0]['correct_answer'].length; i  ) {
  if (question[0]['correct_answer'][i] == answer[i]) {
  • Related