RangeError(索引):无效值:不在0..5范围内(包括6):Flutter SliverChildBuilderDelegate

时间:2019-06-04 06:12:06

标签: android firebase flutter google-cloud-firestore

我在const arr = [ { val : 'abc', arr : [1,2,3,4] }, { val: 'xyz', arr : [7,8,9] } ] const getVal = (arr,key) => (arr.find(x => x.arr.includes(key)) || {}).val; console.log(getVal(arr,3)) console.log(getVal(arr,7))中使用firestore flutter SliverChildBuilderDelegate.建立无限索引。但是我的SliverChildBuilderDelegate只有6个文档。 产生

  

RangeError(索引)错误:无效值:不在0..5范围内,   包含在内:7

我该如何解决?

SliverChildBuilderDelegate中有一个childCount属性,但不起作用 还尝试了偏移量。

firestore

很抱歉,粘贴的格式错误的代码。我尝试了很多,但是可能堆栈不支持飞镖

1 个答案:

答案 0 :(得分:1)

尝试从SliverChildBuilderDelegate参数中删除childCount。而是将childCount直接放在构建器的代码块之后。 (下面是一个查看示例:https://github.com/flutter/flutter/blob/master/examples/flutter_gallery/lib/demo/pesto_demo.dart

    new SliverFixedExtentList(
       itemExtent: 80.0,

    delegate: new SliverChildBuilderDelegate(

    (context, index) => StreamBuilder<QuerySnapshot>(

    stream: Firestore.instance.collection('books').orderBy('open_date')
    .snapshots(),

    builder: (context, snapshot) {

                        print("\n\n\n\n "+snapshot.data.documents.length.toString());
                        print("\n\n\n\n\n\n");
                      if (!snapshot.hasData) return CircularProgressIndicator();
                      else if(index<snapshot.data.documents.length){
                      return Card(
                        child: ListTile(

                          leading: child1,
                          title: Text(snapshot.data.documents[index]['title']),
                          subtitle:
                              Text(snapshot.data.documents[index]['result']),
                        ),
                      );
                      }else{
                      return Text("data");}
                    },
                     childCount:5,
                  ),
            ),
          ),