toastification icon indicating copy to clipboard operation
toastification copied to clipboard

fix animation controller disposed multiple times

Open rebaz94 opened this issue 1 year ago • 3 comments

rebaz94 avatar Apr 30 '24 14:04 rebaz94

Hey @rebaz94 Thanks for your contribution, can you tell us, where did you exactly add your changes? in the animated file?

Also can you send a link to the issue of the widget in flutter repo?

payam-zahedi avatar May 02 '24 19:05 payam-zahedi

Hi @payam-zahedi The fix inside _SliverAnimatedMultiBoxAdaptor class, I only dispose the controller if does not already disposed

@override
  void dispose() {
    for (final _ActiveItem item in _incomingItems.followedBy(_outgoingItems)) {
      if (!item.disposed && item.controller != null) {
        item.disposed = true;
        item.controller!.dispose();
      }
    }
    super.dispose();
  }

and also when removing item it make ensure dispose correctly:

void removeItem(int index, AnimatedRemovedItemBuilder builder, {Duration duration = _kDuration}) {
    assert(index >= 0);

    final int itemIndex = _indexToItemIndex(index);
    assert(itemIndex >= 0 && itemIndex < _itemsCount);
    assert(_activeItemAt(_outgoingItems, itemIndex) == null);

    final _ActiveItem? incomingItem = _removeActiveItemAt(_incomingItems, itemIndex);
    final AnimationController controller =
        incomingItem?.controller ?? AnimationController(duration: duration, value: 1.0, vsync: this);
    final _ActiveItem outgoingItem = _ActiveItem.outgoing(controller, itemIndex, builder);

    // --- added
    if (!mounted) return;

    setState(() {
      _outgoingItems
        ..add(outgoingItem)
        ..sort();
    });

    controller.reverse().then<void>((void value) {
      // --- added
      if (!mounted) return;

      final removedItem = _removeActiveItemAt(_outgoingItems, outgoingItem.itemIndex);
      // --- added
      if (removedItem != null) {
        if (!removedItem.disposed && removedItem.controller != null) {
          removedItem.controller!.dispose();
          removedItem.disposed = true;
        }
      }

      // Decrement the incoming and outgoing item indices to account
      // for the removal.
      for (final _ActiveItem item in _incomingItems) {
        if (item.itemIndex > outgoingItem.itemIndex) {
          item.itemIndex -= 1;
        }
      }
      for (final _ActiveItem item in _outgoingItems) {
        if (item.itemIndex > outgoingItem.itemIndex) {
          item.itemIndex -= 1;
        }
      }

      setState(() => _itemsCount -= 1);
    });
  }

Also can you send a link to the issue of the widget in flutter repo?

Sorry I'm not sure if there is any issue exist for that.

rebaz94 avatar May 02 '24 21:05 rebaz94

Can this be merged? @payam-zahedi

movaid7 avatar May 11 '24 13:05 movaid7

@movaid7 I need to investigate about the side effects

payam-zahedi avatar May 24 '24 07:05 payam-zahedi

Hey @rebaz94 Thanks for your contribution but I think we need to fix this on Flutter soruce code

So I will close this PR and will try to fix it there

payam-zahedi avatar Jul 29 '24 10:07 payam-zahedi

Hey @rebaz94 Thanks for your contribution but I think we need to fix this on Flutter soruce code

So I will close this PR and will try to fix it there

Please link to the Flutter issue once created

movaid7 avatar Jul 29 '24 10:07 movaid7