Skip to content

perf: replace unstable Tile widget key with dedicated stable TileKey#2195

Merged
JaffaKetchup merged 2 commits intofleaflet:masterfrom
braindamagedman:fix/incorrect-tile-keys
Apr 13, 2026
Merged

perf: replace unstable Tile widget key with dedicated stable TileKey#2195
JaffaKetchup merged 2 commits intofleaflet:masterfrom
braindamagedman:fix/incorrect-tile-keys

Conversation

@braindamagedman
Copy link
Copy Markdown
Contributor

Previously tiles were keyed with ObjectKey(tileRenderer), but TileRenderer instances are recreated on every build. Because of that, the key was unstable even when the logical tile did not change. The root cause of this issue was introduced in commit
182c4fc when unbounded horizontal scroll was introduced.

As a result, Flutter could not match the new widget with the existing element and treated the tile as a new widget. This led to repeated disposal and recreation of _TileState instead of updating the existing state.

The fix replaces that key with a stable TileKey based on TileImage identity and positionCoordinates. This preserves _TileState for the same rendered tile and avoids unnecessary remounting.

Before:
before

After fix:
after

You can check simple code to reproduce before/after examples here:
https://github.com/braindamagedman/flutter_map/tree/test/tile-key

@JaffaKetchup
Copy link
Copy Markdown
Member

Oo, thanks for this, we should've caught that. That's a useful debug text, we might add that to the example app.

We need to make sure that this isn't preventing the tiles from changing when we actually do want the state to change, since we've had far too many issues there in the past, but I think your fix should be good.

@JaffaKetchup JaffaKetchup requested a review from a team April 13, 2026 17:34
@JaffaKetchup JaffaKetchup changed the title fix: replace unstable ObjectKey(tileRenderer) with stable TileKey fix: replace unstable Tile widget key with dedicated stable TileKey Apr 13, 2026
Copy link
Copy Markdown
Member

@JaffaKetchup JaffaKetchup left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, LGTM, thanks! I am ofc hesitant in these parts but I think the change itself looks sensible and I can't see any issues with testing. Indeed, it looks like it has improved the performance a bit!

(I don't know why 100% we couldn't have just changed the equality of the tileRenderer, but I think this makes sense anyway.)

@JaffaKetchup JaffaKetchup changed the title fix: replace unstable Tile widget key with dedicated stable TileKey perf: replace unstable Tile widget key with dedicated stable TileKey Apr 13, 2026
@JaffaKetchup JaffaKetchup merged commit 7c973e9 into fleaflet:master Apr 13, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants