Skip to content

Commit 09fd110

Browse files
authored
Use package:http, drop dart:html (#1038)
* Use package:http, drop dart:html * Format
1 parent a3911bb commit 09fd110

File tree

9 files changed

+77
-150
lines changed

9 files changed

+77
-150
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ coverage/
1414
.project
1515

1616
# golden failure diffs
17-
test/failures
17+
**/test/failures
1818
# Flutter crash logs
1919
/flutter_*.log

packages/flutter_svg/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# CHANGES
22

3+
## 2.0.10
4+
5+
- Use package:http for network requests, and allow injection of the client.
6+
- Bump vector_graphics dependency.
7+
38
## 2.0.9
49

510
- Adds back `SvgPicture(theme:)` parameter with a deprecation. Although this

packages/flutter_svg/lib/src/loaders.dart

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import 'dart:convert' show utf8;
33
import 'package:flutter/foundation.dart' hide compute;
44
import 'package:flutter/services.dart';
55
import 'package:flutter/widgets.dart';
6+
import 'package:http/http.dart' as http;
67
import 'package:vector_graphics/vector_graphics.dart';
78
import 'package:vector_graphics_compiler/vector_graphics_compiler.dart' as vg;
89

910
import '../svg.dart' show svg;
1011
import 'default_theme.dart';
1112
import 'utilities/compute.dart';
1213
import 'utilities/file.dart';
13-
import 'utilities/http.dart';
1414

1515
/// A theme used when decoding an SVG picture.
1616
@immutable
@@ -109,6 +109,7 @@ class _DelegateVgColorMapper extends vg.ColorMapper {
109109

110110
/// A [BytesLoader] that parses a SVG data in an isolate and creates a
111111
/// vector_graphics binary representation.
112+
@immutable
112113
abstract class SvgLoader<T> extends BytesLoader {
113114
/// See class doc.
114115
const SvgLoader({
@@ -423,17 +424,21 @@ class SvgNetworkLoader extends SvgLoader<Uint8List> {
423424
this.headers,
424425
super.theme,
425426
super.colorMapper,
426-
});
427+
http.Client? httpClient,
428+
}) : _httpClient = httpClient;
427429

428430
/// The [Uri] encoded resource address.
429431
final String url;
430432

431433
/// Optional HTTP headers to send as part of the request.
432434
final Map<String, String>? headers;
433435

436+
final http.Client? _httpClient;
437+
434438
@override
435-
Future<Uint8List?> prepareMessage(BuildContext? context) {
436-
return httpGet(url, headers: headers);
439+
Future<Uint8List?> prepareMessage(BuildContext? context) async {
440+
final http.Client client = _httpClient ?? http.Client();
441+
return (await client.get(Uri.parse(url), headers: headers)).bodyBytes;
437442
}
438443

439444
@override

packages/flutter_svg/lib/src/utilities/_http_io.dart

-22
This file was deleted.

packages/flutter_svg/lib/src/utilities/_http_web.dart

-12
This file was deleted.

packages/flutter_svg/lib/src/utilities/http.dart

-1
This file was deleted.

packages/flutter_svg/lib/svg.dart

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import 'dart:ui' as ui;
22

33
import 'package:flutter/foundation.dart';
44
import 'package:flutter/widgets.dart';
5+
import 'package:http/http.dart' as http;
56
import 'package:vector_graphics/vector_graphics_compat.dart';
67

78
import 'src/cache.dart';
@@ -248,7 +249,13 @@ class SvgPicture extends StatelessWidget {
248249
this.clipBehavior = Clip.hardEdge,
249250
@deprecated bool cacheColorFilter = false,
250251
SvgTheme? theme,
251-
}) : bytesLoader = SvgNetworkLoader(url, headers: headers, theme: theme),
252+
http.Client? httpClient,
253+
}) : bytesLoader = SvgNetworkLoader(
254+
url,
255+
headers: headers,
256+
theme: theme,
257+
httpClient: httpClient,
258+
),
252259
colorFilter = colorFilter ?? _getColorFilter(color, colorBlendMode),
253260
super(key: key);
254261

packages/flutter_svg/pubspec.yaml

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ 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/dnfield/flutter_svg/tree/master/packages/flutter_svg
44
issue_tracker: https://github.com/dnfield/flutter_svg/issues
5-
version: 2.0.9
5+
version: 2.0.10
66

77
dependencies:
88
flutter:
99
sdk: flutter
10-
vector_graphics: ^1.1.9+1
11-
vector_graphics_codec: ^1.1.9+1
12-
vector_graphics_compiler: ^1.1.9+1
10+
http: ^1.2.1
11+
vector_graphics: ^1.1.11
12+
vector_graphics_codec: ^1.1.11
13+
vector_graphics_compiler: ^1.1.11
1314

1415
dev_dependencies:
1516
flutter_test:

packages/flutter_svg/test/widget_svg_test.dart

+49-105
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import 'dart:async';
21
import 'dart:convert';
3-
import 'dart:io';
42

53
import 'package:flutter/foundation.dart';
64
import 'package:flutter/widgets.dart';
75
import 'package:flutter_svg/flutter_svg.dart';
86

97
import 'package:flutter_test/flutter_test.dart';
8+
import 'package:http/http.dart' as http;
109

1110
class _TolerantComparator extends LocalFileComparator {
1211
_TolerantComparator(Uri testFile) : super(testFile);
@@ -39,9 +38,6 @@ Future<void> _checkWidgetAndGolden(Key key, String filename) async {
3938
}
4039

4140
void main() {
42-
late FakeHttpClientResponse fakeResponse;
43-
late FakeHttpClientRequest fakeRequest;
44-
late FakeHttpClient fakeHttpClient;
4541
final MediaQueryData mediaQueryData =
4642
MediaQueryData.fromView(PlatformDispatcher.instance.implicitView!);
4743

@@ -54,12 +50,6 @@ void main() {
5450
goldenFileComparator = newComparator;
5551
});
5652

57-
setUp(() {
58-
fakeResponse = FakeHttpClientResponse();
59-
fakeRequest = FakeHttpClientRequest(fakeResponse);
60-
fakeHttpClient = FakeHttpClient(fakeRequest);
61-
});
62-
6353
testWidgets(
6454
'SvgPicture does not use a color filtering widget when no color specified',
6555
(WidgetTester tester) async {
@@ -306,40 +296,41 @@ void main() {
306296
});
307297

308298
testWidgets('SvgPicture.network', (WidgetTester tester) async {
309-
await HttpOverrides.runZoned(() async {
310-
final GlobalKey key = GlobalKey();
311-
await tester.pumpWidget(
312-
MediaQuery(
313-
data: mediaQueryData,
314-
child: RepaintBoundary(
315-
key: key,
316-
child: SvgPicture.network(
317-
'test.svg',
318-
),
299+
final GlobalKey key = GlobalKey();
300+
await tester.pumpWidget(
301+
MediaQuery(
302+
data: mediaQueryData,
303+
child: RepaintBoundary(
304+
key: key,
305+
child: SvgPicture.network(
306+
'test.svg',
307+
httpClient: FakeHttpClient(),
319308
),
320309
),
321-
);
322-
await tester.pumpAndSettle();
323-
await _checkWidgetAndGolden(key, 'flutter_logo.network.png');
324-
}, createHttpClient: (SecurityContext? c) => fakeHttpClient);
310+
),
311+
);
312+
await tester.pumpAndSettle();
313+
await _checkWidgetAndGolden(key, 'flutter_logo.network.png');
325314
});
326315

327316
testWidgets('SvgPicture.network with headers', (WidgetTester tester) async {
328-
await HttpOverrides.runZoned(() async {
329-
final GlobalKey key = GlobalKey();
330-
await tester.pumpWidget(
331-
MediaQuery(
332-
data: mediaQueryData,
333-
child: RepaintBoundary(
334-
key: key,
335-
child: SvgPicture.network('test.svg',
336-
headers: const <String, String>{'a': 'b'}),
317+
final GlobalKey key = GlobalKey();
318+
final FakeHttpClient client = FakeHttpClient();
319+
await tester.pumpWidget(
320+
MediaQuery(
321+
data: mediaQueryData,
322+
child: RepaintBoundary(
323+
key: key,
324+
child: SvgPicture.network(
325+
'test.svg',
326+
headers: const <String, String>{'a': 'b'},
327+
httpClient: client,
337328
),
338329
),
339-
);
340-
await tester.pumpAndSettle();
341-
expect(fakeRequest.headers['a']!.single, 'b');
342-
}, createHttpClient: (SecurityContext? c) => fakeHttpClient);
330+
),
331+
);
332+
await tester.pumpAndSettle();
333+
expect(client.headers['a'], 'b');
343334
});
344335

345336
testWidgets('SvgPicture can be created without a MediaQuery',
@@ -361,19 +352,18 @@ void main() {
361352
});
362353

363354
testWidgets('SvgPicture.network HTTP exception', (WidgetTester tester) async {
364-
await HttpOverrides.runZoned(() async {
365-
expect(() async {
366-
fakeResponse.statusCode = 400;
367-
await tester.pumpWidget(
368-
MediaQuery(
369-
data: mediaQueryData,
370-
child: SvgPicture.network(
371-
'notFound.svg',
372-
),
355+
expect(() async {
356+
final http.Client client = FakeHttpClient(400);
357+
await tester.pumpWidget(
358+
MediaQuery(
359+
data: mediaQueryData,
360+
child: SvgPicture.network(
361+
'notFound.svg',
362+
httpClient: client,
373363
),
374-
);
375-
}, isNotNull);
376-
}, createHttpClient: (SecurityContext? c) => fakeHttpClient);
364+
),
365+
);
366+
}, isNotNull);
377367
});
378368

379369
testWidgets('SvgPicture semantics', (WidgetTester tester) async {
@@ -791,65 +781,19 @@ class FakeAssetBundle extends Fake implements AssetBundle {
791781
}
792782
}
793783

794-
class FakeHttpClient extends Fake implements HttpClient {
795-
FakeHttpClient(this.request);
784+
class FakeHttpClient extends Fake implements http.Client {
785+
FakeHttpClient([this.statusCode = 200]);
796786

797-
FakeHttpClientRequest request;
787+
final int statusCode;
798788

799-
@override
800-
Future<HttpClientRequest> getUrl(Uri url) async => request;
801-
}
802-
803-
class FakeHttpHeaders extends Fake implements HttpHeaders {
804-
final Map<String, String?> values = <String, String?>{};
805-
806-
@override
807-
void add(String name, Object value, {bool preserveHeaderCase = false}) {
808-
values[name] = value.toString();
809-
}
789+
final Map<String, String> headers = <String, String>{};
810790

811791
@override
812-
List<String>? operator [](String key) {
813-
return <String>[values[key]!];
814-
}
815-
}
816-
817-
class FakeHttpClientRequest extends Fake implements HttpClientRequest {
818-
FakeHttpClientRequest(this.response);
819-
820-
FakeHttpClientResponse response;
821-
822-
@override
823-
final HttpHeaders headers = FakeHttpHeaders();
824-
825-
@override
826-
Future<HttpClientResponse> close() async => response;
827-
}
828-
829-
class FakeHttpClientResponse extends Fake implements HttpClientResponse {
830-
@override
831-
int statusCode = 200;
832-
833-
@override
834-
int contentLength = svgStr.length;
835-
836-
@override
837-
HttpClientResponseCompressionState get compressionState =>
838-
HttpClientResponseCompressionState.notCompressed;
839-
840-
@override
841-
StreamSubscription<List<int>> listen(
842-
void Function(List<int> event)? onData, {
843-
Function? onError,
844-
void Function()? onDone,
845-
bool? cancelOnError,
846-
}) {
847-
return Stream<Uint8List>.fromIterable(<Uint8List>[svgBytes]).listen(
848-
onData,
849-
onDone: onDone,
850-
onError: onError,
851-
cancelOnError: cancelOnError,
852-
);
792+
Future<http.Response> get(Uri url, {Map<String, String>? headers}) async {
793+
if (headers != null) {
794+
this.headers.addAll(headers);
795+
}
796+
return http.Response(svgStr, statusCode);
853797
}
854798
}
855799

0 commit comments

Comments
 (0)