如何解决颤振中的 RangeError 问题?

问题描述 投票:0回答:1

我写了一个使用下面的 switch 的函数

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();
    }
  }

我正在像下面这样运行的循环中调用该函数

 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]),

但是当我运行应用程序时,我总是收到此范围错误。

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

widget.noOfAttempts 的值等于 1,这应该意味着firstGuessResult 列表不应为空并正常运行,尽管其他列表(secondGuessResult、thirdGuessResult)将为空,但有关使用的代码块这些列表不应运行,也不应生成任何错误,但错误仍然存在。感觉就像我忽略了一些我无法弄清楚的事情。

完整代码供参考

// 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]),
                    ],
                  ),
              ]),
            ),
          ),
        ),
      ),
    );
  }
}
flutter function dart
1个回答
0
投票

您的猜测结果列表之一(firstGuessResults、secondGuessResults、thirdGuessResults)似乎为空或长度小于 [listOfWords],这导致发生此错误。

© www.soinside.com 2019 - 2024. All rights reserved.