Skip to content

Commit 013cc24

Browse files
authored
[flutter_svg] Fix SvgNetworkLoader not closing internal http client (#8126)
closes flutter/flutter#158928 Ensures that if a Client is created in `prepareMessage` it is closed after getting the resource.
1 parent 2703b67 commit 013cc24

File tree

4 files changed

+57
-2
lines changed

4 files changed

+57
-2
lines changed

third_party/packages/flutter_svg/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.0.15
2+
3+
* Fixes `SvgNetworkLoader` not closing internally created http clients.
4+
15
## 2.0.14
26

37
* Makes the package WASM compatible.

third_party/packages/flutter_svg/lib/src/loaders.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,12 @@ class SvgNetworkLoader extends SvgLoader<Uint8List> {
438438
@override
439439
Future<Uint8List?> prepareMessage(BuildContext? context) async {
440440
final http.Client client = _httpClient ?? http.Client();
441-
return (await client.get(Uri.parse(url), headers: headers)).bodyBytes;
441+
final http.Response response =
442+
await client.get(Uri.parse(url), headers: headers);
443+
if (_httpClient == null) {
444+
client.close();
445+
}
446+
return response.bodyBytes;
442447
}
443448

444449
@override

third_party/packages/flutter_svg/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: flutter_svg
22
description: An SVG rendering and widget library for Flutter, which allows painting and displaying Scalable Vector Graphics 1.1 files.
33
repository: https://github.com/flutter/packages/tree/main/third_party/packages/flutter_svg
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_svg%22
5-
version: 2.0.14
5+
version: 2.0.15
66

77
environment:
88
sdk: ^3.4.0

third_party/packages/flutter_svg/test/loaders_test.dart

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'package:flutter/services.dart';
22
import 'package:flutter/widgets.dart';
33
import 'package:flutter_svg/flutter_svg.dart';
44
import 'package:flutter_test/flutter_test.dart';
5+
import 'package:http/http.dart' as http;
56

67
void main() {
78
setUp(() {
@@ -57,6 +58,36 @@ void main() {
5758
expect((await loader.prepareMessage(null))!.lengthInBytes, 0);
5859
expect((await packageLoader.prepareMessage(null))!.lengthInBytes, 1);
5960
});
61+
62+
test('SvgNetworkLoader closes internal client', () async {
63+
final List<VerifyCloseClient> createdClients = <VerifyCloseClient>[];
64+
65+
await http.runWithClient(() async {
66+
const SvgNetworkLoader loader = SvgNetworkLoader('');
67+
68+
expect(createdClients, isEmpty);
69+
await loader.prepareMessage(null);
70+
71+
expect(createdClients, hasLength(1));
72+
expect(createdClients[0].closeCalled, isTrue);
73+
}, () {
74+
final VerifyCloseClient client = VerifyCloseClient();
75+
createdClients.add(client);
76+
return client;
77+
});
78+
});
79+
80+
test("SvgNetworkLoader doesn't close passed client", () async {
81+
final VerifyCloseClient client = VerifyCloseClient();
82+
final SvgNetworkLoader loader = SvgNetworkLoader(
83+
'',
84+
httpClient: client as http.Client,
85+
);
86+
87+
expect(client.closeCalled, isFalse);
88+
await loader.prepareMessage(null);
89+
expect(client.closeCalled, isFalse);
90+
});
6091
}
6192

6293
class TestBundle extends Fake implements AssetBundle {
@@ -96,3 +127,18 @@ class _TestColorMapper extends ColorMapper {
96127
return color;
97128
}
98129
}
130+
131+
class VerifyCloseClient extends Fake implements http.Client {
132+
bool closeCalled = false;
133+
134+
@override
135+
Future<http.Response> get(Uri url, {Map<String, String>? headers}) async {
136+
return http.Response('', 200);
137+
}
138+
139+
@override
140+
void close() {
141+
assert(!closeCalled);
142+
closeCalled = true;
143+
}
144+
}

0 commit comments

Comments
 (0)