From 1c852cedaab61701f527198b5bae6b788d3aa66f Mon Sep 17 00:00:00 2001 From: Dan Field Date: Fri, 20 May 2022 14:51:14 -0700 Subject: [PATCH] Do not assert callbacks contains key if disposed (#104292) --- packages/flutter/lib/src/rendering/layer.dart | 2 +- packages/flutter/test/rendering/layers_test.dart | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/flutter/lib/src/rendering/layer.dart b/packages/flutter/lib/src/rendering/layer.dart index de7fc57e0dd3..9281af79b306 100644 --- a/packages/flutter/lib/src/rendering/layer.dart +++ b/packages/flutter/lib/src/rendering/layer.dart @@ -214,7 +214,7 @@ abstract class Layer extends AbstractNode with DiagnosticableTreeMixin { }()); }; return () { - assert(_callbacks.containsKey(callbackId)); + assert(debugDisposed || _callbacks.containsKey(callbackId)); _callbacks.remove(callbackId); _updateSubtreeCompositionObserverCount(-1); }; diff --git a/packages/flutter/test/rendering/layers_test.dart b/packages/flutter/test/rendering/layers_test.dart index b1f66d929505..e2d7404db75c 100644 --- a/packages/flutter/test/rendering/layers_test.dart +++ b/packages/flutter/test/rendering/layers_test.dart @@ -965,6 +965,21 @@ void main() { expect(root.subtreeHasCompositionCallbacks, false); expect(a1.subtreeHasCompositionCallbacks, false); }); + + test('Double removing a observe callback throws', () { + final ContainerLayer root = ContainerLayer(); + final VoidCallback callback = root.addCompositionCallback((_) { }); + callback(); + + expect(() => callback(), throwsAssertionError); + }); + + test('Removing an observe callback on a disposed layer does not throw', () { + final ContainerLayer root = ContainerLayer(); + final VoidCallback callback = root.addCompositionCallback((_) { }); + root.dispose(); + expect(() => callback(), returnsNormally); + }); } class FakeEngineLayer extends Fake implements EngineLayer {