Skip to content

Commit 38d5dd9

Browse files
authored
Add usage instructions for runWithClient and Flutter (#846)
1 parent 3fba812 commit 38d5dd9

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

pkgs/http/lib/src/client.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,24 @@ Client? get zoneClient {
188188
/// The [Client] returned by [clientFactory] is used by the [Client.new] factory
189189
/// and the convenience HTTP functions (e.g. [http.get]). If [clientFactory]
190190
/// returns `Client()` then the default [Client] is used.
191+
///
192+
/// When used in the context of Flutter, [runWithClient] should be called before
193+
/// [`WidgetsFlutterBinding.ensureInitialized`](https://api.flutter.dev/flutter/widgets/WidgetsFlutterBinding/ensureInitialized.html)
194+
/// because Flutter runs in whatever [Zone] was current at the time that the
195+
/// bindings were initialized.
196+
///
197+
/// [`runApp`](https://api.flutter.dev/flutter/widgets/runApp.html) calls
198+
/// [`WidgetsFlutterBinding.ensureInitialized`](https://api.flutter.dev/flutter/widgets/WidgetsFlutterBinding/ensureInitialized.html)
199+
/// so the easiest approach is to call that in [body]:
200+
/// ```
201+
/// void main() {
202+
/// var clientFactory = Client.new; // Constructs the default client.
203+
/// if (Platform.isAndroid) {
204+
/// clientFactory = MyAndroidHttpClient.new;
205+
/// }
206+
/// runWithClient(() => runApp(const MyApp()), clientFactory);
207+
/// }
208+
/// ```
191209
R runWithClient<R>(R Function() body, Client Function() clientFactory,
192210
{ZoneSpecification? zoneSpecification}) =>
193211
runZoned(body,

0 commit comments

Comments
 (0)