flutter_map_animations icon indicating copy to clipboard operation
flutter_map_animations copied to clipboard

Dispose a map while more than one animateTo is running causes a ticker error

Open JustinMyers opened this issue 2 years ago • 7 comments

The dispose method on AnimatedMapController has the following:

final isAnimating = _animationController?.isAnimating ?? false;

But _animationController is just the most recent animationController.

But if I've triggered two animations in quick succession, and then dispose of the map widget, the previous animation is still running and its ticker from TickerProviderStateMixin is not disposed and it breaks like the following from TickerProvider:

  void dispose() {
    assert(() {
      if (_tickers != null) {
        for (final Ticker ticker in _tickers!) {
          if (ticker.isActive) {
            throw FlutterError.fromParts(<DiagnosticsNode>[
              ErrorSummary('$this was disposed with an active Ticker.'),
              ErrorDescription(
                '$runtimeType created a Ticker via its TickerProviderStateMixin, but at the time '
                'dispose() was called on the mixin, that Ticker was still active. All Tickers must '
                'be disposed before calling super.dispose().',
              ),
              ErrorHint(
                'Tickers used by AnimationControllers '
                'should be disposed by calling dispose() on the AnimationController itself. '
                'Otherwise, the ticker will leak.',
              ),
              ticker.describeForError('The offending ticker was'),
            ]);
          }
        }
      }

Is there a way to cancel AnimatedMapController animations before starting the next one? Is there a better guard against disposing it while more than one animation is running?

Also, I noticed that if one animateTo has a long duration, and you trigger a shorter one, then the fast one will zoom, complete, and then the slower one will take over the map.

Thank you for this library. Apologies if I have done a bad job at describing this issue.

JustinMyers avatar Nov 24 '24 07:11 JustinMyers

I created #44 which seems enough to solve my problem.

JustinMyers avatar Nov 24 '24 08:11 JustinMyers

@JustinMyers Can you please check #45 to confirm that it fixes the ticker error you are encountering.

TesteurManiak avatar Nov 24 '24 11:11 TesteurManiak

@TesteurManiak Hello. #45 does resolve the issue! Thank you.

While we're working in this area of the code I'm wondering if this can also help with the second part of my issue concerning animateTo calls with overlapping durations.

If I animateTo a first location with a duration of 10 seconds, and then start animating to a second location with a duration of 1 second, the second animation takes over the map, completes, and then the map resumes mid-way through the original animation and settles at the first destination when I clearly hoped to settle on the second destination.

My use case is roughly "I want to slowly pan to my destination so I can visualize my walk, but sometimes I want to cancel that slow pan and go back to my start location quickly."

Could we abstract the animation cancelation functionality and expose a way to call it from the AnimatedMapController?

Edit: I now see your comment on #45 re: "Deciding to cancel a running animation when a new one is triggered should be discussed separately". The new PR resolves the dispose issue, yes.

JustinMyers avatar Nov 24 '24 17:11 JustinMyers

My use case is roughly "I want to slowly pan to my destination so I can visualize my walk, but sometimes I want to cancel that slow pan and go back to my start location quickly."

Would adding a parameter cancelPreviousAnimations to the different methods with a default global value that could be set in the constructor enough for your usecase?

AnimatedMapController(
  // ...
  cancelPreviousAnimations: true, // Would default to false.
);

TesteurManiak avatar Nov 24 '24 18:11 TesteurManiak

Yes, I think that would work for the case where I want a fast animation to interrupt an earlier slow one.

Would this give the ability to stop an animation without issuing a new one? In that case would I just call animateTo with no parameters? That should make it cancelPreviousAnimations and just stop where it is, yes?

JustinMyers avatar Nov 24 '24 19:11 JustinMyers

Would this give the ability to stop an animation without issuing a new one? In that case would I just call animateTo with no parameters? That should make it cancelPreviousAnimations and just stop where it is, yes?

I could add a stop() or stopAnimations() method on the controller directly, this method would also be called when the parameter cancelPreviousAnimations is set to true.

TesteurManiak avatar Nov 25 '24 07:11 TesteurManiak

That feels like a better API than making empty animateTo calls. 👍

JustinMyers avatar Nov 25 '24 07:11 JustinMyers