Skip to content

Commit

Permalink
Avoid closing externally created http.Client in network tile provider
Browse files Browse the repository at this point in the history
  • Loading branch information
JaffaKetchup committed Jan 14, 2025
1 parent 3e67dee commit 2241e7b
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/src/layer/tile_layer/tile_provider/network_tile_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class NetworkTileProvider extends TileProvider {
super.headers,
Client? httpClient,
this.silenceExceptions = false,
}) : _httpClient = httpClient ?? RetryClient(Client());
}) : _isInternallyCreatedClient = httpClient == null,
_httpClient = httpClient ?? RetryClient(Client());

/// Whether to ignore exceptions and errors that occur whilst fetching tiles
/// over the network, and just return a transparent tile
Expand All @@ -45,8 +46,14 @@ class NetworkTileProvider extends TileProvider {
/// Long living client used to make all tile requests by
/// [MapNetworkImageProvider] for the duration that this provider is
/// alive
///
/// Not automatically closed if created externally and passed as an argument
/// during construction.
final Client _httpClient;

/// Whether [_httpClient] was created on construction (and not passed in)
final bool _isInternallyCreatedClient;

/// Each [Completer] is completed once the corresponding tile has finished
/// loading
///
Expand Down Expand Up @@ -76,7 +83,8 @@ class NetworkTileProvider extends TileProvider {
if (_tilesInProgress.isNotEmpty) {
await Future.wait(_tilesInProgress.values.map((c) => c.future));
}
_httpClient.close();
if (_isInternallyCreatedClient) _httpClient.close();

super.dispose();
}
}

0 comments on commit 2241e7b

Please sign in to comment.