Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
subhashDev11 opened this issue Oct 26, 2020 · 8 comments
Open

Comments

@subhashDev11
Copy link

when try to remove item with firestrore and streambuilder

@bmuddha
Copy link

bmuddha commented Dec 1, 2020

Same issue, cannot find a workaround, it seems that StickyGroupedListView caches item count, and when the elements list changes (shrinks in this case), it tries to access list with old indexes, disregarding the fact that elements list has fewer items.

This issue happens only in edge case, when items in elements list fit on the screen without causing it to be scrollable.

Also I noticed, that it happens when reverse option is set to true, I think it has to do with the fact that itemCount in ScrollablePositionedList is calculated differently in this case (though it's a mere speculation).

Example:

Expanded(
        child: StickyGroupedListView<Message, DateTime>(
          reverse: true,
          elements: messages,
          order: StickyGroupedListOrder.ASC,
          groupBy: (Message m) {
            final DateTime dt =
                DateTime.fromMillisecondsSinceEpoch(m.creationDate * 1000);
            return DateTime(dt.year, dt.month, dt.day);
          },
          groupComparator: (DateTime value1, DateTime value2) =>
              value2.compareTo(value1),
          itemComparator: (Message m1, Message m2) {
            return m2.creationDate.compareTo(m1.creationDate);
          },
          separator: SizedBox(height: Dim.hm2),
          groupSeparatorBuilder: (Message message) {
            return Container(
              height: Dim.hm3,
              margin: EdgeInsets.symmetric(vertical: Dim.hm2),
              child: Stack(
                children: [
                  Align(
                    alignment: Alignment.center,
                    child: Divider(
                      thickness: 0.0,
                    ),
                  ),
                  Align(
                    // alignment: Alignment.center,
                    child: Container(
                      color: Theme.of(context).scaffoldBackgroundColor,
                      width: Dim.widthPercent(30),
                      child: Padding(
                        padding: const EdgeInsets.all(1.0),
                        child: Text(
                          DateFormatter.getVerboseDate(message.creationDate),
                          style: Theme.of(context).textTheme.subtitle1,
                          textAlign: TextAlign.center,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            );
          },
          itemBuilder: (_, Message message) {
            return ChangeNotifierProvider.value(
              value: message,
              child: MessageTile(message),
            );
          },
        ),
      )

Item removal happens higher in widget tree, and causes rebuild of stateless widget, in which StickyGroupedListView resides.

@bmuddha
Copy link

bmuddha commented Dec 8, 2020

Sort of found a temporary, and far from ideal workaround, may be someone will find it helpful.
Instead of removing the item from source List, just flag them somehow, for example deleted = true;. And then in itemBuilder, just check the property and return an empty container or something like this. In this case the list will still hold the necessary items, and they all will be accessible by the old indices.

@mnarsely
Copy link

This is still an issue, did anyone found a solution?

@waqadArshad
Copy link

waqadArshad commented Jun 23, 2022

@subhashDev11 @bobs4462 @mnarsely @mx1up @Manuelbaun I am having the same issue and along with that sometimes when u delete random values from the list, the list goes blank until a new child is added and the list is refreshed.

Did any of u found a solution?

@mnarsely
Copy link

mnarsely commented Jun 25, 2022

I found a way around it, as the problem for me was the "reverse = true" variable, whenever there are changes on my list I switched the reverse to false to avoid blank screen and then switch back to true after those changes.

@saropa
Copy link

saropa commented Mar 10, 2023

Same issue here - it is still occurring in current release. Unless the list is scrolled to the top (index 0?), then is a fatal RangeError thrown when rebuilding with itemBuilder.

Supporting an optional itemCount property may offer a fix.

@Brechard
Copy link

2 years and a half later this is still happening. in our case, we have reverse set to the default value (false) and it also happens

@tinkie101
Copy link

any update on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants