diff --git a/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart b/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart index 07a201c0f2..6101f8d110 100644 --- a/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart +++ b/packages/flutter/lib/src/widgets/draggable_scrollable_sheet.dart @@ -466,6 +466,13 @@ class _DraggableScrollableSheetScrollPosition } } + @override + void dispose() { + // Stop the animation before dispose. + _ballisticCancelCallback?.call(); + super.dispose(); + } + @override void goBallistic(double velocity) { if (velocity == 0.0 || diff --git a/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart b/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart index db718670d6..27ed9a4c61 100644 --- a/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart +++ b/packages/flutter/test/widgets/draggable_scrollable_sheet_test.dart @@ -344,5 +344,23 @@ void main() { ]; expect(notificationTypes, types); }); + + testWidgets('Do not crash when remove the tree during animation.', (WidgetTester tester) async { + // Regression test for https://github.com/flutter/flutter/issues/89214 + await tester.pumpWidget(_boilerplate( + null, + onScrollNotification: (ScrollNotification notification) { + return false; + }, + )); + + await tester.flingFrom(const Offset(0, 325), const Offset(0, 325), 200); + + // The animation is running. + + await tester.pumpWidget(const SizedBox.shrink()); + + expect(tester.takeException(), isNull); + }); } }