Dimibe/sticky_grouped_list

StateError when scrolling past end of list with AlwaysScrollableScrollPhysics

matt-malarkey opened this issue · 1 comments

When using physics: AlwaysScrollableScrollPhysics() and scrolling up so that the last item is above the groupSeparator for that item, the following error occurs:

======== Exception caught by foundation library ====================================================
The following StateError was thrown while dispatching notifications for ValueNotifier<Iterable<ItemPosition>>:
Bad state: No element

When the exception was thrown, this was the stack: 
#0      Iterable.reduce (dart:core/iterable.dart:353:7)
#1      StickyGroupedListViewState._positionListener (package:sticky_grouped_list/src/sticky_grouped_list.dart:316:10)
#2      ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:324:24)
#3      ValueNotifier.value= (package:flutter/src/foundation/change_notifier.dart:428:5)
#4      _ScrollablePositionedListState._updatePositions (package:scrollable_positioned_list/src/scrollable_positioned_list.dart:571:49)
#5      ChangeNotifier.notifyListeners (package:flutter/src/foundation/change_notifier.dart:324:24)
#6      ValueNotifier.value= (package:flutter/src/foundation/change_notifier.dart:428:5)
#7      _PositionedListState._schedulePositionNotificationUpdate.<anonymous closure> (package:scrollable_positioned_list/src/positioned_list.dart:361:53)
#8      SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1146:15)
#9      SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1091:9)
#10     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:997:5)
#14     _invoke (dart:ui/hooks.dart:151:10)
#15     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:308:5)
#16     _drawFrame (dart:ui/hooks.dart:115:31)

Using sticky_grouped_list: ^3.1.0 the following minimal example can reproduce the issue:

import 'package:flutter/material.dart';
import 'package:sticky_grouped_list/sticky_grouped_list.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Demo')),
        body: StickyGroupedListView<Item, int>(
          physics: const AlwaysScrollableScrollPhysics(),
          elements: [
            Item('1', 1),
            // Item('2', 2),
            // Item('3', 2),
          ],
          groupBy: (Item i) => i.group,
          itemBuilder: (context, i) => ListTile(
            title: Text('Item: ${i.id}'),
            trailing: Text('Group: ${i.group}'),
          ),
          groupSeparatorBuilder: (Item i) => Text('Group ${i.group}'),
        ),
      ),
    );
  }
}

class Item {
  final String id;
  final int group;

  Item(this.id, this.group);
}
w3gw commented

Did you figure out the solution to this issue. I am running in to similar issue. and using sticky_grouped_list: ^3.1.0