From 8136ef0fc0b75b759ea5e02eeaa2e1eac3dca245 Mon Sep 17 00:00:00 2001 From: JaffaKetchup Date: Wed, 7 Aug 2024 09:47:04 +0200 Subject: [PATCH] Fixed bug where reset listener was not fired Added refreshing of reset listener when reset stream changes within widget lifecycle --- lib/src/layer/tile_layer/tile_layer.dart | 25 ++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/src/layer/tile_layer/tile_layer.dart b/lib/src/layer/tile_layer/tile_layer.dart index 09c0e75f5..f0b4db23b 100644 --- a/lib/src/layer/tile_layer/tile_layer.dart +++ b/lib/src/layer/tile_layer/tile_layer.dart @@ -332,8 +332,7 @@ class _TileLayerState extends State with TickerProviderStateMixin { final _tileImageManager = TileImageManager(); late TileBounds _tileBounds; - late var _tileRangeCalculator = - TileRangeCalculator(tileSize: widget.tileSize); + late TileRangeCalculator _tileRangeCalculator; late TileScaleCalculator _tileScaleCalculator; // We have to hold on to the mapController hashCode to determine whether we @@ -345,10 +344,14 @@ class _TileLayerState extends State with TickerProviderStateMixin { StreamSubscription? _tileUpdateSubscription; Timer? _pruneLater; - late final _resetSub = widget.reset?.listen((_) { - _tileImageManager.removeAll(widget.evictErrorTileStrategy); - if (mounted) _loadAndPruneInVisibleBounds(MapCamera.of(context)); - }); + StreamSubscription? _resetSub; + + @override + void initState() { + super.initState(); + _resetSub = widget.reset?.listen(_resetStreamHandler); + _tileRangeCalculator = TileRangeCalculator(tileSize: widget.tileSize); + } // This is called on every map movement so we should avoid expensive logic // where possible, or filter as necessary @@ -452,6 +455,11 @@ class _TileLayerState extends State with TickerProviderStateMixin { } else if (oldWidget.tileDisplay != widget.tileDisplay) { _tileImageManager.updateTileDisplay(widget.tileDisplay); } + + if (widget.reset != oldWidget.reset) { + _resetSub?.cancel(); + _resetSub = widget.reset?.listen(_resetStreamHandler); + } } @override @@ -706,6 +714,11 @@ class _TileLayerState extends State with TickerProviderStateMixin { bool _outsideZoomLimits(num zoom) => zoom < widget.minZoom || zoom > widget.maxZoom; + + void _resetStreamHandler(void _) { + _tileImageManager.removeAll(widget.evictErrorTileStrategy); + if (mounted) _loadAndPruneInVisibleBounds(MapCamera.of(context)); + } } double _distanceSq(TileCoordinates coord, Point center) {