获取此错误 RangeError (Index) Invalid Value: not in Exclusive Range 0..2 : 5 prev

Posted

技术标签:

【中文标题】获取此错误 RangeError (Index) Invalid Value: not in Exclusive Range 0..2 : 5 prev【英文标题】:Getting this Error RangeError (Index) Invalid Value: not in Exclusive Range 0..2 : 5 previou 【发布时间】:2021-11-28 08:43:50 【问题描述】:

之前我得到了 setState() 或 markneedsBuild() 的调用 在构建解决此错误的过程中,我调用了一个函数 initState() 现在我收到范围错误。谁来解决这个我在 学习 paise 我对 Flutter 了解不够我是初学者 我 在下面提供了我所有文件的代码

 **main.dart**       
            
        
  

 // ignore_for_file: prefer_const_constructors
      
        import 'package:flutter/material.dart';
        import 'package:flutter/scheduler.dart';
        import './questions.dart';
        import './answer.dart';
        
        void main() 
          runApp(Myapp());
        
        
        class Myapp extends StatefulWidget 
          @override
          State<StatefulWidget> createState() 
            return MyappState();
          
        
        class MyappState extends State<Myapp> 
          var questionIndex = 0;
          @override
          void initState() 
            SchedulerBinding.instance!.addPostFrameCallback((_) 
              // fetch data
             answerChosen();
            );
          
          answerChosen() 
            setState(() 
              questionIndex = questionIndex + 1;
            );
            
            // ignore: avoid_print
             print(questionIndex);
          
           @override
          Widget build(BuildContext context) 
            var questions = [
              
                'QuestionText': 'what is your favorite color',
                'Answers': ['red', 'greed', 'blue', 'Black']
              ,
              
                'QuestionText': 'what\'s your favorite animal',
                'Answers': ['cat', 'dog', 'lion', 'girgit']
              ,
              
                'QuestionText': 'Who\'s your favorite Player',
                'Answers': ['Messi', 'Neymar', 'Ronaldo', 'Other']
              
            ];
            return MaterialApp(
              home: Scaffold(
                appBar: AppBar(
                  title: Text('My First App'),
                ),
                body: Column(
                  children: [
                    Question(
                      questions[questionIndex]['QuestionText'] as String,
                    ),
                    ...(questions[questionIndex]['Answers'] as List<String>).map(
                      (answer) 
                        return jawab(answerChosen, answer);
                      ,
                    ).toList()
                  ],
                ),
              ),
            );
          
        
    

answer.dart

 import 'package:flutter/material.dart';
        class Answer extends StatelessWidget 
          final Function selectHandler;
          final String answerText;
           Answer(this.selectHandler, this.answerText);
          @override
          Widget build(BuildContext context) 
            return Container(
              width: double.infinity,
              child: RaisedButton(
                color: Colors.blue,
                textColor: Colors.white,
                child: Text(answerText),
                onPressed: selectHandler(),
              ),
            );
          
        
    
   

questions.dart

import 'package:flutter/material.dart';
        class Question extends StatelessWidget 
          final String questionText;
        
          Question(this.questionText);
        
          @override
          Widget build(BuildContext context) 
            return Container(
              width: double.infinity,
              margin: EdgeInsets.all(10),
              child: Text(
                questionText,
                style: TextStyle(fontSize: 28),
                textAlign: TextAlign.center,
              ),
            );
          
        

【问题讨论】:

【参考方案1】:

您正在尝试匹配超出问题范围的索引。

它的最大索引是 2。但是当你选择一个答案并且你在最后一个问题上时,questionIndex 会变成 3。

answerChosen() 
  // if it's on last index
  if(questions.length - 1 == questionIndex) 
    // do something else
    return;
  

  setState(() 
    questionIndex = questionIndex + 1;
  );

把问题放到build方法外面,这样就可以拿到最后一个了 从 answerChosen 的第一行开始,条件中的索引是动态的。

希望我的回答对你有所帮助! :)

【讨论】:

以上是关于获取此错误 RangeError (Index) Invalid Value: not in Exclusive Range 0..2 : 5 prev的主要内容,如果未能解决你的问题,请参考以下文章

FLUTTER:RangeError(索引):索引超出范围:没有有效索引:0(通过参数)

颤振错误:RangeError(索引):无效值:不在0..2范围内,包括:3

RangeError index out of range

Flutter:DioError [DioErrorType.DEFAULT]:RangeError(索引):无效值:只有有效值为0:

RangeError,语法错误:无效的正则表达式

颤振:RangeError(索引):无效值:不在包含范围内0..27:28