Caching
since v8.2
flutter_map supports integration of basic map tile caching (with compatible tile providers) through caching providers & provides an automatically-enabled implementation on non-web platforms, known as built-in caching.
Caching aims to:
Improve developer experience by:
Reducing the costs of using tile servers, by reducing duplicate tile requests
Keep your app lightweight - the built-in cache doesn't ship any binaries or databases, just a couple extra libraries you probably already use
Improve user experience by:
Reducing tile loading durations, as fetching from the cache is very quick
Reducing network/Internet usage, which may be limited or metered/expensive (eg. mobile broadband)
Improve compliance with tile server requirements, by reducing the strain on them
Be extensible, customizable, and integrate with multiple tile providers
It does, however, come at the expense of usage of on-device storage capacity.
Offline mapping and caching can also be implemented in other ways. See Offline Mapping for more information.
Configuring the built-in provider
To configure the default provider, provide arguments to the getOrCreateInstance factory constructor. Usually this is done when constructing the TileLayer/TileProvider:
By default, caching occurs in a platform provided cache directory. The operating system may clear this at any time.
By default, a 1 GB (soft) limit is applied to the built-in caching. This limit is only applied when the cache provider is initialised (usually when the first tiles are loaded on each app session).
HTTP headers are used to determine how long a tile is considered 'fresh' - this fulfills the requirements of many tile servers. However, setting overrideFreshAge allows the HTTP headers to be overridden, and the tile to be stored and used for a set duration.
The tileKeyGenerator can be customized. The callback accepts the tile's URL, and converts it to a key used to uniquely identify the tile. By default, it generates a UUID from the entire URL string. However, in some cases, the default behaviour should be changed:
Deleting the cache
With the default BuiltInMapCachingProvider, it is possible to delete the cache contents in two ways:
When the app is running,
destroythe current instance and set thedeleteCacheargument totrue(then optionally create a new instance if required, which happens automatically on the next tile load by default)When the app is not running, users may delete the storage directory
If the default cache directory is used, users may do this by 'clearing the app cache' through their operating system, for example. On some platforms, this may need to be done manually (which may be difficult for less technical users), whilst on others, it may be a simple action.
Using other providers
You can also use any other MapCachingProvider implementation, such as provided by plugins, or create one yourself! They may support the web platform, unlike the built-in cache.
You should check that plugin's documentation for information about initialisation & configuration. You will always need to pass it to the cachingProvider argument of a compatible TileProvider.
Disabling built-in caching
If you prefer to disable built-in caching, use the DisabledMapCachingProvider on each tile provider:
Last updated
Was this helpful?