Ashish-Raturi / scroll_loop_auto_scroll

This widget automatically scrolls the custom child widget to an infinite loop.
MIT License
19 stars 7 forks source link

ScrollController listener invokes callback post-disposal leading to errors #7

Closed hsalem7 closed 1 year ago

hsalem7 commented 1 year ago

Problem

When a widget equipped with ScrollLoopAutoScroll is mounted and then immediately disposed, an error is triggered due to the scroll controller being accessed after the widget's disposal.

After investigating, I discovered that there's an intrinsic delay before initiating the scrolling. If the widget is mounted and subsequently unmounted prior to the culmination of this delay, it results in an error.

Error Message:

flutter: 'package:flutter/src/widgets/scroll_controller.dart': Failed assertion: line 105 pos 12: '_positions.isNotEmpty': ScrollController not attached to any scroll views.

Proposed Solution

To prevent this error, it's pivotal to ensure that the widget is still mounted before activating the animation within the scroll controller's listener. Here's an example:

scrollController.addListener(() async {
      if (widget.enableScrollInput) {
        if (animationController.isAnimating) {
          animationController.stop();
        } else {
          await Future.delayed(widget.delayAfterScrollInput);
          if (mounted) {
            animationHandler();
          }
        }
      }
    });

I will submit a pull request addressing this issue shortly.