Flutter ListView RangeError (index): Invalid value: Not in inclusive range 0..19: 20

474 Views Asked by At

I have a list view with a dynamic length that's provided by a streambuilder :

StreamBuilder<List>(
          initialData: [],
          stream: applicants.stream,
          builder: (context, AsyncSnapshot<List> snapshot) {
            return Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: <Widget>[
                  Expanded(
                      child: ListView.builder(
                          controller: _scrollController,
                          itemCount: snapshot.data.length,
                          itemBuilder: (context, index) {  print('length of applicants' +
                                snapshot.data.length.toString());
                            final applicant =
                                snapshot.data.elementAt(index);return Container(applicant.name);}

and I have the following behavior subject which has the list of applicants.

BehaviorSubject<List> applicants = BehaviorSubject<List>.seeded([]);
ScrollController _scrollController = ScrollController();
@override
void initState() {
super.initState();

_scrollController.addListener(() {
  if (_scrollController.position.atEdge) {
    if (_scrollController.position.pixels == 0) {
    } else {
      print('you are at bottom position');
      //Fire event to get more applicants
       applicants.sink
          .add(applicants.value + newApplicants);
    }
  }
});}

The problem is that whenever i scroll down the list it gives me this error:

The following RangeError was thrown building:
flutter: RangeError (index): Invalid value: Not in inclusive range 0..19: 20

Thank you!

0

There are 0 best solutions below