flutter_map_tile_caching icon indicating copy to clipboard operation
flutter_map_tile_caching copied to clipboard

[BUG] Tiles not updated when `urlTemplate` changes if tile provider not reconstructed

Open codeOfJannik opened this issue 11 months ago • 4 comments

What is the bug?

When a FMTCTileProvider is used and the urlTemplate is switched during runtime, the rendered map tiles on the currently visible map area are not updated to the style of the new url template. Also emitting an event to the reset stream of the TileLayer does not force the layer to show the new style. But if you move around the map, new map tiles are rendered in the new style. So switching the url template in general works.

How can we reproduce it?

  • Take the flutter_map 8.1.0 example app
  • add flutter_map_tile_caching as a dependency
  • add FMTC init and setup to main.dart:
WidgetsFlutterBinding.ensureInitialized();
await FMTCObjectBoxBackend().initialise();
await const FMTCStore('mapStore').manage.create();
  • to lib/pages/reset_tile_layer.dart add the FMTCTileProvider logic:
final _fmtcTileProvider = FMTCTileProvider(stores: {'mapStore': BrowseStoreStrategy.readUpdateCreate}, httpClient: httpClient);

final httpClient = IOClient(HttpClient()..userAgent = null);

class ResetTileLayerPage extends StatefulWidget {
TileLayer(
    reset: resetController.stream,
    urlTemplate: layerToggle ? layer1 : layer2,
    subdomains: layerToggle ? const [] : const ['a', 'b', 'c'],
    userAgentPackageName: 'dev.fleaflet.flutter_map.example',
    tileProvider: _fmtcTileProvider,
),

Do you have a potential solution?

No response

Platforms

tested on iOS device and Android emulator

Severity

Obtrusive: Prevents normal functioning but causes no errors in the console

codeOfJannik avatar Mar 04 '25 13:03 codeOfJannik

Hi @codeOfJannik, Thanks for reporting, I can reproduce this, and I've found why/where the issue occurs. I'll look into fixing it.

JaffaKetchup avatar Mar 05 '25 22:03 JaffaKetchup

#182 has been updated to include the best solution to this problem, following the pattern of the FM tile providers. However, it relies upon a breaking change. I do not intend to release a breaking change yet, as the purpose/aim of the PR may be changed by an upcoming change to FM.

The issue occurs because the Flutter image cache key assigned to the tile does not consider the URL template - this was missed during testing, since in the example app, it does not occur because the provider is reconstructed every time the URL is changed, and is not easy to fix without a breaking change because the internal image provider takes the entire TileLayer as a parameter (from which it then generates the tile's URL), which does not have an equality, so it is not tested. Therefore, the image cache does not realise it needs a new image, so just displays the one cached in memory.

There's a few workaround options:

  • When you change the URL, reconstruct a new FMTCTileProvider - you may also need to change a property (a dummy header field or a new HTTP client might work well) - to break the equality chain
  • Override your FMTC dependency to the 'support-vector' branch from Git (you shouldn't be affected by any changes, but since it does technically change the external API, it is considered breaking even if no-one is likely to be using that API)
  • In the coming day(s) I'll try to release a v11 prerelease containing those changes, which you can then depend on (but will be more stable than depending from Git as you won't receive any further commits which change things unexpectedly)

JaffaKetchup avatar Mar 05 '25 23:03 JaffaKetchup

@JaffaKetchup Thanks for investigating and providing the workaround options!

codeOfJannik avatar Mar 06 '25 08:03 codeOfJannik

I've released v11.0.0-dev.2, so you can depend on that instead of Git (if you were using that option). It is unlikely to progress to a full release soon, see the description on the #182 PR.

JaffaKetchup avatar Mar 09 '25 18:03 JaffaKetchup