RangeError (index): Invalid value: Not in inclusive range 0..1: 2

时间:2021-04-13 02:51:38

标签: flutter dart

在这个应用程序中,我试图根据 'listen' 事件获取查询。如果数据库中有任何变化,移动屏幕中的数据也会发生变化。 下面是从数据库中获取数据的代码。

  quizStart(){
    FirebaseFirestore.instance.collection('quizes').doc(docId).get().then((value) {
      length = value.data()['questions'].length;
    });

    ref = FirebaseFirestore.instance.collection('quizes').doc(docId).collection('sessions').doc(recId).snapshots().listen((event) {
      var op = event.data();
      print('getData: $length');
      if(length == op['session']['index']){
        print('length: $length op: ${op['session']['index']}');
        showExitDialog('You have Completed the Quizi...');
      }else
      if(op['session']['active'] == false)
        showExitDialog('The Quizi has been ended..');
      else
       if(op['session']['createdAt'] == null )
        showDialog();
      else {
        Future.delayed(Duration(seconds: 3));
        Navigator.pushReplacement(
            context,
            MaterialPageRoute(
                builder: (context) => AnswersPage(
                    docId: docId, recId: recId, playerName: playerName)));
      
      }
    });
  }

我正在使用 Gridview 构建器显示数据。

StreamBuilder(
                stream: sessionQuery,
                builder: (context,AsyncSnapshot<DocumentSnapshot> snapshot){
                  if(snapshot.hasError)
                    return Text('Error: ${snapshot.error}');
                  switch (snapshot.connectionState){
                    case ConnectionState.waiting: return Text('Loading ...');
                    default: queIndex = snapshot.data['session']['index'];
                    return StreamBuilder(
                      stream: questionTypeQuery,
                        builder: (BuildContext context,AsyncSnapshot snap){
                          if(snap == null && snap.hasError)
                            return Text('Error: ${snap.error}');
                          switch (snap.connectionState){
                            case ConnectionState.waiting: return Text('Quizi! is Loading ... Please Wait');
                            default:
                           answerType = snap.data['questions'][queIndex]['answertype'].toString();
                              points = snap.data['questions'][queIndex]['points'].toString();
                              question =snap.data['questions'][queIndex]['question'].toString();
                              quesLength = snap.data['questions'][queIndex]['answers'].length;
                                return RubberBand(
                                  key: gridAnim,
                                  child: GridView.builder(
                                    scrollDirection: Axis.vertical,
                                    shrinkWrap: true,
                                    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount:2),
                                    itemCount: quesLength,
                                    itemBuilder: (BuildContext context, int index){
                                      return InkWell(
                                          onTap: (){
                                         showDialog('Look Out for the next question...');
                                          },

                                          child: Padding(
                                              padding: const EdgeInsets.all(15.0),
                                              child: Material(
                                                elevation: 5,
                                                borderRadius: new BorderRadius.circular(20.0),
                                                child: Container(
                                                  decoration: BoxDecoration(
                                                    boxShadow: [
                                                      BoxShadow(
                                                        color: Colors.yellowAccent.withOpacity(0.5),
                                                        offset: const Offset(1.0, 1.0,), blurRadius: 5.0, spreadRadius: 0.5,),
                                                      BoxShadow(color: Colors.white,
                                                        offset: const Offset(0.0, 0.0), blurRadius: 0.0, spreadRadius: 0.0,), ],
                                                    border: Border.all(color: Colors.lightGreenAccent, width: 2.0, style: BorderStyle.solid),
                                                    color: Colors.deepPurple[500], borderRadius: new BorderRadius.circular(20.0),
                                                  ),
                                               child: Center(child:
                                                  Text(snap.data['questions'][queIndex]['answers'][index]['answer'].toString(),
                                                      style: GoogleFonts.bubblegumSans(color: Colors.white, fontSize: 25,))),
                                                ),
                                              )

                                          ));
                                    },
                                  ),
                                );
                          }
                  });
                }}
              ),

我在上面代码的第 9 行出现错误。

即使在 quizStart() 中工作的条件我也收到范围错误。

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我只是用来显示第二个 Streambuilder。

(前)

If(condition)
 return Widget
else
 return StreamBuilder.

现在我得到了所需的输出。

我不知道这是针对此错误的正确解决方案。但这对我有用。

谢谢。