Replies: 1 comment 2 replies
-
Hi @vnmiso, thanks for reaching out! I do agree that we need to document the fact that tile providers aren't automatically reusable, and are attached to the It is potentially worth saying that swapping a We do recommend in the docs that users should pass in their own HTTP client in the case that their Regarding the bug in the I'll try to come up with some concise way to represent the decision tree for the positioning of the |
Beta Was this translation helpful? Give feedback.
-
I wasn't sure if I should add an issue for this so I'll let a maintainer decide.
There's a fundamental issue with
TileProvider
: its lifetime is tied to aTileLayer
's state's lifetime. How so? Because_TileLayerState.dispose()
callswidget.tileProvider.dispose()
. Notice that thetileProvider
is held in thewidget
, akaTileLayer
. In Flutter, this typically means that the widget doesn't own the passed in argument (unless explicitly stated by the documentation, but it isn't), so it shouldn't be responsible for managing its lifecycle.This means that
TileProvider
s are not reusable (also not mentioned in docs). If aTileProvider
instance is ever passed into aTileLayer
, it's going to be disposed whenever theTileLayer
is removed from the tree.Consider the following code:
When you press the button, tile loads will fail because
NetworkTileProvider
's internally constructed http client is closed in itsdispose()
. Since flutter_map v8, if you pass in your own http client you won't have this problem, but before v8 the externally passed in http clients were still closed so it still wouldn't work.So you could say that I'm doing it wrong, and instead I should construct a fresh instance of
TileProvider
in every build, like so:But if you do it this way (and let's assume this is the only correct way), then you should be canonically expected to ALWAYS pass in your own http client. Why? Let's see.
If you don't pass in your own http client, a new one will be created on every build, which is already bad by itself, but you also lose the nice connection pool you had in the previous http client. And there's a bug in
_TileLayerState
: only the lastTileProvider
that it sees (throughwidget.tileProvider
) is ever disposed, previous instances and their http clients will remain undisposed. (To solve this, intermediatetileProvider
s should be disposed indidUpdateWidget()
, so I guess we do need a bug issue -- ifTileLayer
will remain the owner of the passed inTileProvider
s.)So my conclusion is we probably need some docs around this stuff for
TileProvider
, and decide on who's responsible for aTileProvider's
lifecycle. I'll try to make a PR after this is discussed andif I have time, but most likely I won't :(P.S. I want to say a huge thanks to all the maintainers and contributors of
flutter_map
for this wonderful package, without which I wouldn't be able to do my work as well as with other Flutter mapping solutions.Beta Was this translation helpful? Give feedback.
All reactions