[BUG] `NetworkTileProvider` has poorer performance and new unhandled exceptions since v6
What is the bug?
Sometimes not all the tiles are being loaded/drawn. After moving/dragging the map all the tiles are immediately drawn/refreshed
How can we reproduce it?
It might be me with all the code in my project, but try with these tiles: TileLayer( urlTemplate: "https://{s}.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}.png", subdomains: ['a', 'b', 'c', 'd'], tileProvider: NetworkTileProvider(), keepBuffer: 10, maxZoom: 23, retinaMode: false);
Do you have a potential solution?
A workaround: add a method in MapController to refresh/redraw the tiles on demand.
Platforms
All
Severity
Obtrusive: Prevents normal functioning but causes no errors in the console
Can I ask what version you are running?
flutter_map: ^6.0.0 flutter: 3.13.7/8 dart: 3.1.4
Does the issue occur with the OSM tile server? (Note: only use the OSM tile server for testing.)
I don't use OSM (I use https://{s}.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}.png). Can try OSM and update.
I don't know if it's related but I'm running a local OSM server and having updated to flutter map v6 have noticed tiles take longer to display than with flutter map v5. I'm testing on Windows 11 native. Particularly when quickly zooming in and out, grey tiles are visible for a second or so in many places, when previously it would be almost instant (given local server). Nothing's changed on the server or it's caching.
I got the same problem.I upgraded flutter_map from V4 to V6, then the console showed ImageCodecException :Failed to detect image file format using the file header.
File header was [0x3c 0x21 0x44 0x4f 0x43 0x54 0x59 0x50 0x45 0x20].
I found it was because the tile requested that didn't exist on Server or Assets.In V4 it throws a 404 NOT FOUND error,but in V6 it throws a ImageCodecException . I don't know in V6 why it always gave a strange fixed byte data when the tile request doesn't exist on server .To solve this problem just restrict CameraConstraint . But even the console shows no error log, some tiles are still not shown correctly just like @vitalsh 's problem.
On a related note, the flutter map throws an exception each time it can not load a tile, and that makes debugging applications with a flutter_map widget, too troublesome. It constantly throws Connection closed before header was received, Socket Exception, and similar server-related errors and pauses the debug session. A fix or workaround for this would be great. This is happening in both OSM and MapBox tile servers.
@manlyman29 Are you also using the standard tile provider? What version are you running, and does it occur in earlier versions?
@swust-xl That sounds possibly like a different problem.
(Deleted out of office response from you)
On a related note, the flutter map throws an exception each time it can not load a tile, and that makes debugging applications with a flutter_map widget, too troublesome. It constantly throws
Connection closed before header was received,Socket Exception, and similar server-related errors and pauses the debug session. A fix or workaround for this would be great. This is happening in both OSM and MapBox tile servers.
To confirm I also get this if I just stop my local OSM server. Every time flutter map tries to load tiles (startup, move, zoom, etc.) I get something similar to this on the VS Code debug console:
flutter: (OS Error: The remote computer refused the network connection.
flutter: , errno = 1225), address = localhost, port = 64972, uri=http://localhost:8080/tile/18/130142/87058.png
flutter: ClientException with SocketException: The remote computer refused the network connection.
At various times (I don't think every move, but most?) I also get:
══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════
The following _ClientSocketException was thrown resolving an image codec:
ClientException with SocketException: The remote computer refused the network connection.
(OS Error: The remote computer refused the network connection.
, errno = 1225), address = localhost, port = 64838, uri=http://localhost:8080/tile/3/4/3.png
When the exception was thrown, this was the stack:
#0 IOClient.send (package:http/src/io_client.dart:119:7)
<asynchronous suspension>
#1 RetryClient.send (package:http/retry.dart:115:20)
<asynchronous suspension>
#2 BaseClient._sendUnstreamed (package:http/src/base_client.dart:93:32)
<asynchronous suspension>
#3 BaseClient.readBytes (package:http/src/base_client.dart:58:22)
<asynchronous suspension>
#4 FlutterMapNetworkImageProvider._loadAsync (package:flutter_map/src/layer/tile_layer/tile_provider/network_image_provider.dart:84:11)
<asynchronous suspension>
URL: http://localhost:8080/tile/3/4/3.png
Fallback URL: null
Current provider: FlutterMapNetworkImageProvider()
════════════════════════════════════════════════════════════════════════════════════════════════════
...followed by lots of:
Another exception was thrown: ClientException with SocketException: The remote computer refused the network connection.
I don't recall the behaviour in version 5, but I don't remember having this stream of exceptions each time!
I'm not sure why you didn't see these before, but that's normal (possibly if you were using a remote URL before it kept retrying or never received a proper response somehow, but now you're on localhost your machine knows there's nothing there)!
If you use a NetworkImage with an unreachable URL, it spits out a similar message as far as I can remember.
We MUST return an image or an error from inside an image provider. We could return a transparent tile instead of an error, but that would just hide the problem and potentially cause more issues. I guess the way to solve it now that we're using logger elsewhere is to create a custom error log, but then we're back to the same issue.
An unreachable URL or 404 response causing errors in console is OK. A 404 error attempting and failing to be decoded (invalid codec) as an image is not OK, and probably means we have a logic gap in our custom image provider - but I'll need to check that.
the custom image provider has historically thrown exceptions for network errors correct. in my app i use a custom tile provider that caches the tiles locally on disk. i need to re-write it soon with the new image decoder, but at the moment it also throws exceptions for network errors. , which can be annoying while debugging.
mine explicitly throws for all non-ok status codes. I believe my custom network provider is in the docs somewhere as the caching tiles example?
if (response.statusCode != HttpStatus.ok) {
throw NetworkImageLoadException(
statusCode: response.statusCode, uri: url);
}
@JaffaKetchup Thanks for the reply, Yes it occurs on the standard NetworkTileProvider, with v6.0.0
I am not completely sure about earlier versions, I think this used to happen but a lot less.
Most of the time exception is from here:
Edit: Just noticed the CancellableNetworkTileProvider, Similar issue of constantly throwing an exception DioException [request cancelled]: The request was cancelled.
I see what you're saying regarding the necessity of warning the user/program. I wonder if there's a way of reducing the frequency somehow though. Maybe after 10 tile exceptions in a row pause for a bit (and have a comment saying so?). It's just it seems while one exception/error is helpful, a massive stream of them is almost the opposite. I understand it's important to pick up cases where only one or two aren't loading, so a minimum threshold and pause might do both?
Maybe this isn't a priority as most people's URLs will be fine and they'll have a net connection, but thought I'd share my view given I'm getting notifications for this issue anyway 😉
We could possibly return a custom image that just is red and has the text "Failure", but that seems like a lot of effort, and at that point, we should bring back the failure image argument. It also seems a bit OTT, but maybe that's the best option. Let me know what you think.
We could also add an argument to the provider to just ignore errors and return a transparent tile - and maybe combining this with the above might work.
(I know it's not always helpful, but I always use "Run Without Debugging" from VS Code. Contrary to the name, all of Flutter's DevTools are available, along with hot reload/restart, it just doesn't support breakpoints, and errors are just left in console.)
The reason it may be appearing more is because we reinforced our error catching in this region, as it was ignoring some errors before, which caused some issues. Possibly, it's because we're now throwing every error caught inside the catchError block (which didn't exist before), rather than rethrowing (because we can't inside catchError).
The comment https://github.com/fleaflet/flutter_map/issues/1698#issuecomment-1774564973 from above about a decoding error with 404 seems to be a different issue. With the changes to error handling in this area, we are now catching async errors directly from decode, rather than just using try/catch. Essentially, we're catching the error at decode time rather than response time, although I'm not entirely sure why this happens with some URLs and not others.
I've rewritten the _loadAsync method to be a little more flexible, reduce duplication, and stop mixing sync & async code:
httpClient
.readBytes(
Uri.parse(useFallback ? fallbackUrl ?? '' : url),
headers: headers,
)
.then((bytes) => ImmutableBuffer.fromUint8List(bytes))
.then((buffer) => decode(buffer))
.catchError((dynamic err) {
// ignore: only_throw_errors
if (useFallback || fallbackUrl == null) throw err as Object;
return _loadAsync(key, chunkEvents, decode, useFallback: true);
});
I can't see a difference, but maybe there'll be a difference for some people.
It's also worth noting that this is more customizable. Rather than using a catch-all catchError, we could use the onError callback inside each then to handle each error more specifically - but I can't really see the point in that for now.
Open to other suggestions to change the way this works.
Of course, the ideal method would be to copy the implementation of NetworkImage closely, but that does involve a separate implementation for IO and web. I'll see if I can do it.
the readBytes function returns a ClientException if the status code is not success, so that would explain why the 404 status codes are getting thrown up
Yeah, but I thought we had that in earlier version tbh. Turns out, copying Flutter's network image implementation is quite difficult as they go quite deep into the behind-the-scenes implementations, particularly on the web.
@mattmyne @manlyman29 @swust-xl Please use #1703 to discuss anything about error handling. This issue should solely be used for its original purpose about reporting a performance issue with the default image provider.