Skip to content

Commit

Permalink
Add usage instructions for runWithClient and Flutter (#846)
Browse files Browse the repository at this point in the history
  • Loading branch information
brianquinlan authored Dec 30, 2022
1 parent 3fba812 commit 38d5dd9
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkgs/http/lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,24 @@ Client? get zoneClient {
/// The [Client] returned by [clientFactory] is used by the [Client.new] factory
/// and the convenience HTTP functions (e.g. [http.get]). If [clientFactory]
/// returns `Client()` then the default [Client] is used.
///
/// When used in the context of Flutter, [runWithClient] should be called before
/// [`WidgetsFlutterBinding.ensureInitialized`](https://api.flutter.dev/flutter/widgets/WidgetsFlutterBinding/ensureInitialized.html)
/// because Flutter runs in whatever [Zone] was current at the time that the
/// bindings were initialized.
///
/// [`runApp`](https://api.flutter.dev/flutter/widgets/runApp.html) calls
/// [`WidgetsFlutterBinding.ensureInitialized`](https://api.flutter.dev/flutter/widgets/WidgetsFlutterBinding/ensureInitialized.html)
/// so the easiest approach is to call that in [body]:
/// ```
/// void main() {
/// var clientFactory = Client.new; // Constructs the default client.
/// if (Platform.isAndroid) {
/// clientFactory = MyAndroidHttpClient.new;
/// }
/// runWithClient(() => runApp(const MyApp()), clientFactory);
/// }
/// ```
R runWithClient<R>(R Function() body, Client Function() clientFactory,
{ZoneSpecification? zoneSpecification}) =>
runZoned(body,
Expand Down

0 comments on commit 38d5dd9

Please sign in to comment.