Skip to content

Commit 1c852ce

Browse files
authored
Do not assert callbacks contains key if disposed (#104292)
1 parent 6631e63 commit 1c852ce

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

packages/flutter/lib/src/rendering/layer.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ abstract class Layer extends AbstractNode with DiagnosticableTreeMixin {
214214
}());
215215
};
216216
return () {
217-
assert(_callbacks.containsKey(callbackId));
217+
assert(debugDisposed || _callbacks.containsKey(callbackId));
218218
_callbacks.remove(callbackId);
219219
_updateSubtreeCompositionObserverCount(-1);
220220
};

packages/flutter/test/rendering/layers_test.dart

+15
Original file line numberDiff line numberDiff line change
@@ -965,6 +965,21 @@ void main() {
965965
expect(root.subtreeHasCompositionCallbacks, false);
966966
expect(a1.subtreeHasCompositionCallbacks, false);
967967
});
968+
969+
test('Double removing a observe callback throws', () {
970+
final ContainerLayer root = ContainerLayer();
971+
final VoidCallback callback = root.addCompositionCallback((_) { });
972+
callback();
973+
974+
expect(() => callback(), throwsAssertionError);
975+
});
976+
977+
test('Removing an observe callback on a disposed layer does not throw', () {
978+
final ContainerLayer root = ContainerLayer();
979+
final VoidCallback callback = root.addCompositionCallback((_) { });
980+
root.dispose();
981+
expect(() => callback(), returnsNormally);
982+
});
968983
}
969984

970985
class FakeEngineLayer extends Fake implements EngineLayer {

0 commit comments

Comments
 (0)