简体   繁体   中英

RangeError (index): Invalid value: Not in range 0..5, inclusive: 6 : Flutter SliverChildBuilderDelegate

I was using using firestore with flutter SliverChildBuilderDelegate. SliverChildBuilderDelegate building a infinite index. but my firestore has only 6 documents. resulting

an error of RangeError (index): Invalid value: Not in range 0..5, inclusive: 7

how can I solve this?

There is a childCount property in SliverChildBuilderDelegate but not working also tried the offset.

new SliverFixedExtentList(
       itemExtent: 80.0,

delegate: new SliverChildBuilderDelegate(

(context, index ,{childCount:5}) => 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");}
                    },
                  ),
            ),
          ),

Sorry for pasting a wrong formatted code. I tried a lot but may be stack doesn't support flutter dart

Try removing the childCount from the SliverChildBuilderDelegate arguments. Instead, place childCount directly after your code block for builder. (here is an example to look at: 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,
                  ),
            ),
          ),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM