diff --git a/packages/flame/lib/src/components/text_box_component.dart b/packages/flame/lib/src/components/text_box_component.dart index 2e50212e93b..5252bf1409f 100644 --- a/packages/flame/lib/src/components/text_box_component.dart +++ b/packages/flame/lib/src/components/text_box_component.dart @@ -270,14 +270,20 @@ class TextBoxComponent extends TextComponent { } } + final Set cachedToRemove = {}; + Future redraw() async { final newSize = _recomputeSize(); final cachedImage = cache; - if (cachedImage != null) { + if (cachedImage != null && !cachedToRemove.contains(cachedImage)) { + cachedToRemove.add(cachedImage); // Do not dispose of the cached image immediately, since it may have been // sent into the rendering pipeline where it is still pending to be used. // See issue #1618 for details. - Future.delayed(const Duration(milliseconds: 100), cachedImage.dispose); + Future.delayed(const Duration(milliseconds: 100), () { + cachedToRemove.remove(cachedImage); + cachedImage.dispose(); + }); } cache = await _fullRenderAsImage(newSize); size = newSize;