skip to Main Content

Theres a funtion i wrote that uses switch below

Widget buildVerdict(BuildContext context, bool firstGuessResult,
      bool? secondGuessResult, bool? thirdGuessResult) {
    switch (widget.noOfAttempts) {
      case 1:
        return firstGuessResult == true
            ? const Icon(
                Icons.check_box,
                color: Colors.green,
              )
            : Container();
      case 2:
        return firstGuessResult == true && secondGuessResult == true
            ? const Icon(
                Icons.check_box,
                color: Colors.green,
              )
            : Container();
      case 3:
        return firstGuessResult == true &&
                secondGuessResult == true &&
                thirdGuessResult == true
            ? const Icon(
                Icons.check_box,
                color: Colors.green,
              )
            : Container();
      default:
        return Container();
    }
  }

I am calling the function inside a loop that runs like below

 for (int i = 0; i < widget.listOfWords.length; i++)
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        widget.listOfWords[i],
                        style: TextStyle(
                          color: widget.firstGuessResults[i] == true
                              ? Colors.green
                              : Colors.redAccent,
                        ),
                      ),
                      widget.failedSecondAttempt
                          ? Text(
                              widget.listOfWords[i],
                              style: TextStyle(
                                color: widget.secondGuessResults[i] == true
                                    ? Colors.green
                                    : Colors.redAccent,
                              ),
                            )
                          : Container(),
                      widget.failedThirdAttempt
                          ? Text(
                              widget.listOfWords[i],
                              style: TextStyle(
                                color: widget.thirdGuessResults[i] == true
                                    ? Colors.green
                                    : Colors.redAccent,
                              ),
                            )
                          : Container(),
                      buildVerdict(context,
                          widget.firstGuessResults[i],
                          widget.secondGuessResults[i],
                          widget.thirdGuessResults[i]),

But i always get this Range error when i run the app.

The following IndexError was thrown building Results(dirty, state: _ResultsState#3bf32):
RangeError (index): Index out of range: no indices are valid: 0

The value of widget.noOfAttempts is equal to one, this should mean that the firstGuessResult list should not be empty and run as normal, though the other lists (secondGuessResult, thirdGuessResult) would be empty, but the block of code concerning the use of those lists should not run and no error should be generated, yet the error persists. it feels like i am overlooking something that i cant quite figure out.

Full code for reference

// ignore_for_file: must_be_immutable, deprecated_member_use

import 'package:flutter/material.dart';

class Results extends StatefulWidget {
  bool failedSecondAttempt;
  bool failedThirdAttempt;
  int noOfAttempts;
  final List listOfWords;
  List<bool> firstGuessResults;
  List<bool> secondGuessResults;
  List<bool> thirdGuessResults;

  Results({
    super.key,
    required this.failedSecondAttempt,
    required this.failedThirdAttempt,
    required this.listOfWords,
    required this.noOfAttempts,
    required this.firstGuessResults,
    required this.secondGuessResults,
    required this.thirdGuessResults,
  });

  @override
  State<Results> createState() => _ResultsState();
}

class _ResultsState extends State<Results> {
  @override
  void initState() {
    super.initState();
  }

  Widget buildVerdict(BuildContext context, bool firstGuessResult,
      bool? secondGuessResult, bool? thirdGuessResult) {
    switch (widget.noOfAttempts) {
      case 1:
        return firstGuessResult == true
            ? const Icon(
                Icons.check_box,
                color: Colors.green,
              )
            : Container();
      case 2:
        return firstGuessResult == true && secondGuessResult == true
            ? const Icon(
                Icons.check_box,
                color: Colors.green,
              )
            : Container();
      case 3:
        return firstGuessResult == true &&
                secondGuessResult == true &&
                thirdGuessResult == true
            ? const Icon(
                Icons.check_box,
                color: Colors.green,
              )
            : Container();
      default:
        return Container();
    }
  }
  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: WillPopScope(
        onWillPop: () async {
          return false;
        },
        child: Scaffold(
          body: Padding(
            padding: const EdgeInsets.all(8.0),
            child: SingleChildScrollView(
              physics: const AlwaysScrollableScrollPhysics(),
              child: Column(children: [
                Center(
                  child: ElevatedButton(
                      onPressed: () {
                        Navigator.pop(context, true);
                      },
                      child: const Text('Close')),
                ),
                for (int i = 0; i < widget.listOfWords.length; i++)
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        widget.listOfWords[i],
                        style: TextStyle(
                          color: widget.firstGuessResults[i] == true
                              ? Colors.green
                              : Colors.redAccent,
                        ),
                      ),
                      widget.failedSecondAttempt
                          ? Text(
                              widget.listOfWords[i],
                              style: TextStyle(
                                color: widget.secondGuessResults[i] == true
                                    ? Colors.green
                                    : Colors.redAccent,
                              ),
                            )
                          : Container(),
                      widget.failedThirdAttempt
                          ? Text(
                              widget.listOfWords[i],
                              style: TextStyle(
                                color: widget.thirdGuessResults[i] == true
                                    ? Colors.green
                                    : Colors.redAccent,
                              ),
                            )
                          : Container(),
                      buildVerdict(context,
                          widget.firstGuessResults[i],
                          widget.secondGuessResults[i],
                          widget.thirdGuessResults[i]),
                    ],
                  ),
              ]),
            ),
          ),
        ),
      ),
    );
  }
}

2

Answers


  1. It looks likes one of your Guess Result list(firstGuessResults, secondGuessResults, thirdGuessResults) is empty or have length less than [listOfWords] which is causing this error to occur.

    Login or Signup to reply.
  2. You can try to call your Results with this input:

    class ResultsScreen extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        List<String> listOfWords = ['Word1', 'Word2', 'Word3'];
        List<bool> firstGuessResults = [true, false, true];
        List<bool> secondGuessResults = [false, true, true];
        List<bool> thirdGuessResults = [true, true, false];
    
        return Results(
          failedSecondAttempt: true,
          failedThirdAttempt: true,
          listOfWords: listOfWords,
          noOfAttempts: 3,
          firstGuessResults: firstGuessResults,
          secondGuessResults: secondGuessResults,
          thirdGuessResults: thirdGuessResults,
        );
      }
    }
    

    Normally it could be work.
    I think that one of firstGuessResults, secondGuessResults, thirdGuessResults is empty. (check also if listOfWords is empty)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search