Skip to content

Commit 661f5d6

Browse files
authored
Use package:http_image_provider in all Client implementation examples (#1089)
1 parent 473a892 commit 661f5d6

File tree

20 files changed

+126
-115
lines changed

20 files changed

+126
-115
lines changed

.github/workflows/cronet.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
- name: Make cronet_http_embedded copy
4040
if: ${{ matrix.package == 'cronet_http_embedded' }}
4141
run: |
42-
cp -r pkgs/cronet_http pkgs/cronet_http_embedded
42+
mv pkgs/cronet_http pkgs/cronet_http_embedded
4343
cd pkgs/cronet_http_embedded
4444
flutter pub get && dart tool/prepare_for_embedded.dart
4545
- id: install

pkgs/cronet_http/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.0.1-wip
2+
3+
* Use `package:http_image_provider` in the example application.
4+
15
## 1.0.0
26

37
* No functional changes.

pkgs/cronet_http/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import 'package:http/http.dart';
3737
import 'package:http/io_client.dart';
3838
3939
void main() async {
40-
late Client httpClient;
40+
final Client httpClient;
4141
if (Platform.isAndroid) {
4242
final engine = CronetEngine.build(
4343
cacheMode: CacheMode.memory,

pkgs/cronet_http/example/lib/book.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class Book {
66
String title;
77
String description;
8-
String imageUrl;
8+
Uri imageUrl;
99

1010
Book(this.title, this.description, this.imageUrl);
1111

@@ -21,7 +21,7 @@ class Book {
2121
'description': final String description,
2222
'imageLinks': {'smallThumbnail': final String thumbnail}
2323
}) {
24-
books.add(Book(title, description, thumbnail));
24+
books.add(Book(title, description, Uri.parse(thumbnail)));
2525
}
2626
}
2727
}

pkgs/cronet_http/example/lib/main.dart

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,31 @@
55
import 'dart:convert';
66
import 'dart:io';
77

8-
import 'package:cached_network_image/cached_network_image.dart';
98
import 'package:cronet_http/cronet_http.dart';
109
import 'package:flutter/material.dart';
1110
import 'package:http/http.dart';
11+
import 'package:http/io_client.dart';
12+
import 'package:http_image_provider/http_image_provider.dart';
13+
import 'package:provider/provider.dart';
1214

1315
import 'book.dart';
1416

1517
void main() {
16-
var clientFactory = Client.new; // Constructs the default client.
18+
final Client httpClient;
1719
if (Platform.isAndroid) {
1820
final engine = CronetEngine.build(
19-
cacheMode: CacheMode.memory, userAgent: 'Book Agent');
20-
clientFactory = () => CronetClient.fromCronetEngine(engine);
21+
cacheMode: CacheMode.memory,
22+
cacheMaxSize: 2 * 1024 * 1024,
23+
userAgent: 'Book Agent');
24+
httpClient = CronetClient.fromCronetEngine(engine);
25+
} else {
26+
httpClient = IOClient(HttpClient()..userAgent = 'Book Agent');
2127
}
22-
runWithClient(() => runApp(const BookSearchApp()), clientFactory);
28+
29+
runApp(Provider<Client>(
30+
create: (_) => httpClient,
31+
child: const BookSearchApp(),
32+
dispose: (_, client) => client.close()));
2333
}
2434

2535
class BookSearchApp extends StatelessWidget {
@@ -44,20 +54,22 @@ class HomePage extends StatefulWidget {
4454
class _HomePageState extends State<HomePage> {
4555
List<Book>? _books;
4656
String? _lastQuery;
57+
late Client _client;
4758

4859
@override
4960
void initState() {
5061
super.initState();
62+
_client = context.read<Client>();
5163
}
5264

5365
// Get the list of books matching `query`.
5466
// The `get` call will automatically use the `client` configurated in `main`.
5567
Future<List<Book>> _findMatchingBooks(String query) async {
56-
final response = await get(
68+
final response = await _client.get(
5769
Uri.https(
5870
'www.googleapis.com',
5971
'/books/v1/volumes',
60-
{'q': query, 'maxResults': '40', 'printType': 'books'},
72+
{'q': query, 'maxResults': '20', 'printType': 'books'},
6173
),
6274
);
6375

@@ -129,11 +141,10 @@ class _BookListState extends State<BookList> {
129141
itemBuilder: (context, index) => Card(
130142
key: ValueKey(widget.books[index].title),
131143
child: ListTile(
132-
leading: CachedNetworkImage(
133-
placeholder: (context, url) =>
134-
const CircularProgressIndicator(),
135-
imageUrl:
136-
widget.books[index].imageUrl.replaceFirst('http', 'https')),
144+
leading: Image(
145+
image: HttpImage(
146+
widget.books[index].imageUrl.replace(scheme: 'https'),
147+
client: context.read<Client>())),
137148
title: Text(widget.books[index].title),
138149
subtitle: Text(widget.books[index].description),
139150
),

pkgs/cronet_http/example/pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ environment:
77
sdk: ^3.0.0
88

99
dependencies:
10-
cached_network_image: ^3.2.3
1110
cronet_http:
1211
path: ../
1312
cupertino_icons: ^1.0.2
1413
flutter:
1514
sdk: flutter
1615
http: ^1.0.0
16+
http_image_provider: ^0.0.2
17+
provider: ^6.1.1
1718

1819
dev_dependencies:
1920
dart_flutter_team_lints: ^2.0.0

pkgs/cronet_http/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: cronet_http
2-
version: 1.0.0
2+
version: 1.0.1-wip
33
description: >-
44
An Android Flutter plugin that provides access to the Cronet HTTP client.
55
repository: https://github.com/dart-lang/http/tree/master/pkgs/cronet_http

pkgs/cupertino_http/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.2.1-wip
2+
3+
* Use `package:http_image_provider` in the example application.
4+
15
## 1.2.0
26

37
* Add support for setting additional http headers in

pkgs/cupertino_http/README.md

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,49 +24,25 @@ This approach allows the same HTTP code to be used on all platforms, while
2424
still allowing platform-specific setup.
2525

2626
```dart
27-
late Client client;
28-
if (Platform.isIOS) {
29-
final config = URLSessionConfiguration.ephemeralSessionConfiguration()
30-
..allowsCellularAccess = false
31-
..allowsConstrainedNetworkAccess = false
32-
..allowsExpensiveNetworkAccess = false;
33-
client = CupertinoClient.fromSessionConfiguration(config);
34-
} else {
35-
client = IOClient(); // Uses an HTTP client based on dart:io
36-
}
37-
38-
final response = await client.get(Uri.https(
39-
'www.googleapis.com',
40-
'/books/v1/volumes',
41-
{'q': 'HTTP', 'maxResults': '40', 'printType': 'books'}));
42-
```
43-
44-
[package:http runWithClient][] can be used to configure the
45-
[package:http Client][] for the entire application.
46-
47-
```dart
48-
void main() {
49-
late Client client;
50-
if (Platform.isIOS) {
51-
client = CupertinoClient.defaultSessionConfiguration();
27+
import 'package:cupertino_http/cupertino_http.dart';
28+
import 'package:http/http.dart';
29+
import 'package:http/io_client.dart';
30+
31+
void main() async {
32+
final Client httpClient;
33+
if (Platform.isIOS || Platform.isMacOS) {
34+
final config = URLSessionConfiguration.ephemeralSessionConfiguration()
35+
..cache = URLCache.withCapacity(memoryCapacity: 2 * 1024 * 1024)
36+
..httpAdditionalHeaders = {'User-Agent': 'Book Agent'};
37+
httpClient = CupertinoClient.fromSessionConfiguration(config);
5238
} else {
53-
client = IOClient();
39+
httpClient = IOClient(HttpClient()..userAgent = 'Book Agent');
5440
}
5541
56-
runWithClient(() => runApp(const MyApp()), () => client);
57-
}
58-
59-
...
60-
61-
class MainPageState extends State<MainPage> {
62-
void someMethod() {
63-
// Will use the Client configured in main.
64-
final response = await get(Uri.https(
65-
'www.googleapis.com',
66-
'/books/v1/volumes',
67-
{'q': 'HTTP', 'maxResults': '40', 'printType': 'books'}));
68-
}
69-
...
42+
final response = await client.get(Uri.https(
43+
'www.googleapis.com',
44+
'/books/v1/volumes',
45+
{'q': 'HTTP', 'maxResults': '40', 'printType': 'books'}));
7046
}
7147
```
7248

@@ -88,6 +64,5 @@ task.resume();
8864
```
8965

9066
[package:http Client]: https://pub.dev/documentation/http/latest/http/Client-class.html
91-
[package:http runWithClient]: https://pub.dev/documentation/http/latest/http/runWithClient.html
9267
[Foundation URL Loading System]: https://developer.apple.com/documentation/foundation/url_loading_system
9368
[dart:io HttpClient]: https://api.dart.dev/stable/dart-io/HttpClient-class.html

pkgs/cupertino_http/example/lib/book.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class Book {
66
String title;
77
String description;
8-
String imageUrl;
8+
Uri imageUrl;
99

1010
Book(this.title, this.description, this.imageUrl);
1111

@@ -21,7 +21,7 @@ class Book {
2121
'description': final String description,
2222
'imageLinks': {'smallThumbnail': final String thumbnail}
2323
}) {
24-
books.add(Book(title, description, thumbnail));
24+
books.add(Book(title, description, Uri.parse(thumbnail)));
2525
}
2626
}
2727
}

pkgs/cupertino_http/example/lib/main.dart

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,30 @@
55
import 'dart:convert';
66
import 'dart:io';
77

8-
import 'package:cached_network_image/cached_network_image.dart';
98
import 'package:cupertino_http/cupertino_http.dart';
109
import 'package:flutter/material.dart';
1110
import 'package:http/http.dart';
11+
import 'package:http/io_client.dart';
12+
import 'package:http_image_provider/http_image_provider.dart';
13+
import 'package:provider/provider.dart';
1214

1315
import 'book.dart';
1416

1517
void main() {
16-
var clientFactory = Client.new; // The default Client.
18+
final Client httpClient;
1719
if (Platform.isIOS || Platform.isMacOS) {
1820
final config = URLSessionConfiguration.ephemeralSessionConfiguration()
1921
..cache = URLCache.withCapacity(memoryCapacity: 2 * 1024 * 1024)
2022
..httpAdditionalHeaders = {'User-Agent': 'Book Agent'};
21-
clientFactory = () => CupertinoClient.fromSessionConfiguration(config);
23+
httpClient = CupertinoClient.fromSessionConfiguration(config);
24+
} else {
25+
httpClient = IOClient(HttpClient()..userAgent = 'Book Agent');
2226
}
23-
runWithClient(() => runApp(const BookSearchApp()), clientFactory);
27+
28+
runApp(Provider<Client>(
29+
create: (_) => httpClient,
30+
child: const BookSearchApp(),
31+
dispose: (_, client) => client.close()));
2432
}
2533

2634
class BookSearchApp extends StatelessWidget {
@@ -45,20 +53,22 @@ class HomePage extends StatefulWidget {
4553
class _HomePageState extends State<HomePage> {
4654
List<Book>? _books;
4755
String? _lastQuery;
56+
late Client _client;
4857

4958
@override
5059
void initState() {
5160
super.initState();
61+
_client = context.read<Client>();
5262
}
5363

5464
// Get the list of books matching `query`.
5565
// The `get` call will automatically use the `client` configurated in `main`.
5666
Future<List<Book>> _findMatchingBooks(String query) async {
57-
final response = await get(
67+
final response = await _client.get(
5868
Uri.https(
5969
'www.googleapis.com',
6070
'/books/v1/volumes',
61-
{'q': query, 'maxResults': '40', 'printType': 'books'},
71+
{'q': query, 'maxResults': '20', 'printType': 'books'},
6272
),
6373
);
6474

@@ -130,11 +140,10 @@ class _BookListState extends State<BookList> {
130140
itemBuilder: (context, index) => Card(
131141
key: ValueKey(widget.books[index].title),
132142
child: ListTile(
133-
leading: CachedNetworkImage(
134-
placeholder: (context, url) =>
135-
const CircularProgressIndicator(),
136-
imageUrl:
137-
widget.books[index].imageUrl.replaceFirst('http', 'https')),
143+
leading: Image(
144+
image: HttpImage(
145+
widget.books[index].imageUrl.replace(scheme: 'https'),
146+
client: context.read<Client>())),
138147
title: Text(widget.books[index].title),
139148
subtitle: Text(widget.books[index].description),
140149
),

pkgs/cupertino_http/example/pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ environment:
1010
flutter: '>=3.10.0'
1111

1212
dependencies:
13-
cached_network_image: ^3.2.3
1413
cupertino_http:
1514
path: ../
1615
cupertino_icons: ^1.0.2
1716
flutter:
1817
sdk: flutter
1918
http: ^1.0.0
19+
http_image_provider: ^0.0.2
20+
provider: ^6.1.1
2021

2122
dev_dependencies:
2223
convert: ^3.1.1

pkgs/cupertino_http/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: cupertino_http
2-
version: 1.2.0
2+
version: 1.2.1-wip
33
description: >-
44
A macOS/iOS Flutter plugin that provides access to the Foundation URL
55
Loading System.

pkgs/flutter_http_example/README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ A Flutter sample app that illustrates how to configure and use
99
including:
1010

1111
* configuration for multiple platforms.
12-
* using `runWithClient` and `package:provider` to pass `Client`s through
13-
an application.
12+
* using `package:provider` to pass `Client`s through an application.
1413
* writing tests using `MockClient`.
1514

1615
## The important bits
@@ -34,9 +33,8 @@ This library demonstrates how to:
3433

3534
* import `http_client_factory.dart` or `http_client_factory_web.dart`,
3635
depending on whether we are targeting the web browser or not.
37-
* share a `package:http` `Client` by using `runWithClient` and
38-
`package:provider`.
39-
* call `package:http` functions.
36+
* share a `package:http` `Client` by using `package:provider`.
37+
* call `package:http` `Client` methods.
4038

4139
### `widget_test.dart`
4240

pkgs/flutter_http_example/lib/book.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class Book {
66
String title;
77
String description;
8-
String imageUrl;
8+
Uri imageUrl;
99

1010
Book(this.title, this.description, this.imageUrl);
1111

@@ -21,7 +21,7 @@ class Book {
2121
'description': final String description,
2222
'imageLinks': {'smallThumbnail': final String thumbnail}
2323
}) {
24-
books.add(Book(title, description, thumbnail));
24+
books.add(Book(title, description, Uri.parse(thumbnail)));
2525
}
2626
}
2727
}

0 commit comments

Comments
 (0)