File tree Expand file tree Collapse file tree 4 files changed +20
-22
lines changed
cupertino_http/example/lib Expand file tree Collapse file tree 4 files changed +20
-22
lines changed Original file line number Diff line number Diff line change @@ -8,21 +8,16 @@ import 'dart:io';
8
8
import 'package:cronet_http/cronet_client.dart' ;
9
9
import 'package:flutter/material.dart' ;
10
10
import 'package:http/http.dart' ;
11
- import 'package:http/io_client.dart' ;
12
11
13
12
import 'book.dart' ;
14
13
15
- void main () async {
16
- late Client client;
14
+ void main () {
15
+ var clientFactory = Client . new ; // Constructs the default client.
17
16
if (Platform .isAndroid) {
18
- client = CronetClient (await CronetEngine .build (
19
- cacheMode: CacheMode .memory, cacheMaxSize: 1024 * 1024 ));
20
- } else {
21
- client = IOClient ();
17
+ WidgetsFlutterBinding .ensureInitialized ();
18
+ clientFactory = CronetClient .new ;
22
19
}
23
-
24
- // Run the app with the default `client` set to the one assigned above.
25
- runWithClient (() => runApp (const BookSearchApp ()), () => client);
20
+ runWithClient (() => runApp (const BookSearchApp ()), clientFactory);
26
21
}
27
22
28
23
class BookSearchApp extends StatelessWidget {
Original file line number Diff line number Diff line change @@ -8,21 +8,15 @@ import 'dart:io';
8
8
import 'package:cupertino_http/cupertino_client.dart' ;
9
9
import 'package:flutter/material.dart' ;
10
10
import 'package:http/http.dart' ;
11
- import 'package:http/io_client.dart' ;
12
11
13
12
import 'book.dart' ;
14
13
15
14
void main () {
16
- late Client client;
17
- // Use Cupertino Http on iOS and macOS.
15
+ var clientFactory = Client .new ; // The default Client.
18
16
if (Platform .isIOS || Platform .isMacOS) {
19
- client = CupertinoClient .defaultSessionConfiguration ();
20
- } else {
21
- client = IOClient ();
17
+ clientFactory = CupertinoClient .defaultSessionConfiguration.call;
22
18
}
23
-
24
- // Run the app with the default `client` set to the one assigned above.
25
- runWithClient (() => runApp (const BookSearchApp ()), () => client);
19
+ runWithClient (() => runApp (const BookSearchApp ()), clientFactory);
26
20
}
27
21
28
22
class BookSearchApp extends StatelessWidget {
Original file line number Diff line number Diff line change @@ -171,8 +171,11 @@ Client? get zoneClient {
171
171
/// }
172
172
///
173
173
/// void main() {
174
- /// Client client = Platform.isAndroid ? MyAndroidHttpClient() : Client();
175
- /// runWithClient(myFunction, () => client);
174
+ /// var clientFactory = Client.new; // Constructs the default client.
175
+ /// if (Platform.isAndroid) {
176
+ /// clientFactory = MyAndroidHttpClient.new;
177
+ /// }
178
+ /// runWithClient(myFunction, clientFactory);
176
179
/// }
177
180
///
178
181
/// void myFunction() {
@@ -183,7 +186,8 @@ Client? get zoneClient {
183
186
/// ```
184
187
///
185
188
/// The [Client] returned by [clientFactory] is used by the [Client.new] factory
186
- /// and the convenience HTTP functions (e.g. [http.get] )
189
+ /// and the convenience HTTP functions (e.g. [http.get] ). If [clientFactory]
190
+ /// returns `Client()` then the default [Client] is used.
187
191
R runWithClient <R >(R Function () body, Client Function () clientFactory,
188
192
{ZoneSpecification ? zoneSpecification}) =>
189
193
runZoned (body,
Original file line number Diff line number Diff line change @@ -153,6 +153,11 @@ void main() {
153
153
expect (client, isA <TestClient >());
154
154
});
155
155
156
+ test ('runWithClient Client() return' , () {
157
+ final client = http.runWithClient (() => http.Client (), () => http.Client ());
158
+ expect (client, isA< http_io.IOClient > ());
159
+ });
160
+
156
161
test ('runWithClient nested' , () {
157
162
late final http.Client client;
158
163
late final http.Client nestedClient;
You can’t perform that action at this time.
0 commit comments