Skip to content

Commit c14c92e

Browse files
authored
Better document the runWithClient(() => ..., client.call) pattern (#772)
1 parent c7ee0ef commit c14c92e

File tree

4 files changed

+20
-22
lines changed

4 files changed

+20
-22
lines changed

pkgs/cronet_http/example/lib/main.dart

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,16 @@ import 'dart:io';
88
import 'package:cronet_http/cronet_client.dart';
99
import 'package:flutter/material.dart';
1010
import 'package:http/http.dart';
11-
import 'package:http/io_client.dart';
1211

1312
import 'book.dart';
1413

15-
void main() async {
16-
late Client client;
14+
void main() {
15+
var clientFactory = Client.new; // Constructs the default client.
1716
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;
2219
}
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);
2621
}
2722

2823
class BookSearchApp extends StatelessWidget {

pkgs/cupertino_http/example/lib/main.dart

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,15 @@ import 'dart:io';
88
import 'package:cupertino_http/cupertino_client.dart';
99
import 'package:flutter/material.dart';
1010
import 'package:http/http.dart';
11-
import 'package:http/io_client.dart';
1211

1312
import 'book.dart';
1413

1514
void main() {
16-
late Client client;
17-
// Use Cupertino Http on iOS and macOS.
15+
var clientFactory = Client.new; // The default Client.
1816
if (Platform.isIOS || Platform.isMacOS) {
19-
client = CupertinoClient.defaultSessionConfiguration();
20-
} else {
21-
client = IOClient();
17+
clientFactory = CupertinoClient.defaultSessionConfiguration.call;
2218
}
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);
2620
}
2721

2822
class BookSearchApp extends StatelessWidget {

pkgs/http/lib/src/client.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,11 @@ Client? get zoneClient {
171171
/// }
172172
///
173173
/// 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);
176179
/// }
177180
///
178181
/// void myFunction() {
@@ -183,7 +186,8 @@ Client? get zoneClient {
183186
/// ```
184187
///
185188
/// 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.
187191
R runWithClient<R>(R Function() body, Client Function() clientFactory,
188192
{ZoneSpecification? zoneSpecification}) =>
189193
runZoned(body,

pkgs/http/test/io/client_test.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ void main() {
153153
expect(client, isA<TestClient>());
154154
});
155155

156+
test('runWithClient Client() return', () {
157+
final client = http.runWithClient(() => http.Client(), () => http.Client());
158+
expect(client, isA<http_io.IOClient>());
159+
});
160+
156161
test('runWithClient nested', () {
157162
late final http.Client client;
158163
late final http.Client nestedClient;

0 commit comments

Comments
 (0)