【问题标题】:Getting this Error RangeError (Index) Invalid Value: not in Exclusive Range 0..2 : 5 previou获取此错误 RangeError (Index) Invalid Value: not in Exclusive Range 0..2 : 5 prev
【发布时间】: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,
              ),
            );
          }
        }

【问题讨论】:

    标签: flutter dart widget flutter-layout dart-null-safety


    【解决方案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 的第一行开始,条件中的索引是动态的。

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

    【讨论】:

      猜你喜欢
      • 2019-11-21
      • 2019-08-24
      • 2023-02-10
      • 2022-08-17
      • 2022-01-18
      • 2022-07-18
      • 2019-11-25
      • 1970-01-01
      • 2018-05-24
      相关资源
      最近更新 更多