The-ring-io / flutter_staggered_animations

Easily add staggered animations to your ListView, GridView, Column and Row children.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A ScrollController was used after being disposed.

anisalibegic opened this issue · comments

A ScrollController was used after being disposed.

When the exception was thrown, this was the stack: 
#0      ChangeNotifier._debugAssertNotDisposed.<anonymous closure> (package:flutter/src/foundation/change_notifier.dart:105:9)
#1      ChangeNotifier._debugAssertNotDisposed (package:flutter/src/foundation/change_notifier.dart:111:6)
#2      ChangeNotifier.addListener (package:flutter/src/foundation/change_notifier.dart:141:12)
#3      _FadingEdgeScrollViewState.initState (package:fading_edge_scrollview/src/fading_edge_scrollview.dart:128:17)

_controller.addListener(_onScroll); in the _FadingEdgeScrollViewState

This happens when I'm refreshing my list. Everything works great the first time. As soon as the rebuild happens it throws an error.

Hello @grandpa-guru,
I don't think this exception is related to the use of this package...

I found the problem which I don't know how to fix. I'm using fading_edge_scrollview as a child of flutter_staggered_animations and FadingEdgeScrollView.fromScrollView requires that the controller inside the ListView is assigned, it can't be null. And also, child has to be of type ScrollView. So in the end my code looks like this:

AnimationLimiter(
  child: FadingEdgeScrollView.fromScrollView(
    child: ListView.separated(
      itemBuilder: (BuildContext context, int index) {
        return AnimationConfiguration.staggeredList(
          child: SlideAnimation(
            child: FadeInAnimation(
              child: ...,
            ),
            verticalOffset: 50.0,
          ),
          position: index,
          duration: const Duration(milliseconds: 375),
        );
      },
      separatorBuilder: (BuildContext context, int index) {
        return ...;
      },
      itemCount: ...,
      controller: _controller,
    ),
  ),
);

Everything is working great if I get rid of the FadingEdgeScrollView. I suppose that the FadingEdgeScrollView disposes the controller when rebuilding and then the AnimationLimiter can't work with disposed controller. What to do in this situation?

You can try to remove the AnimationLimiter and see if the exception still occurs...
Is everything working as expected if you entirely remove animations ?

I solved it by creating an instance of ScrollController every rebuild instead of final widget property.
controller: ScrollController(),

It's not the best way but I looked at the source code of the FadingEdgeScrollView and it disposes the controller when needed so I'm sure that there is no memory leaks. Thanks for the help.