Skip to content

Commit 321362a

Browse files
authored
Document that runWithClient should not be used with flutter (#1249)
1 parent bf96551 commit 321362a

File tree

1 file changed

+11
-19
lines changed

1 file changed

+11
-19
lines changed

pkgs/http/lib/src/client.dart

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -202,34 +202,26 @@ Client? get zoneClient {
202202
/// HTTP requests made using `dart:io` or `dart:html` APIs,
203203
/// or using specifically instantiated client implementations, are not affected.
204204
///
205-
/// When used in the context of Flutter, [runWithClient] should be called before
206-
/// [`WidgetsFlutterBinding.ensureInitialized`](https://api.flutter.dev/flutter/widgets/WidgetsFlutterBinding/ensureInitialized.html)
207-
/// because Flutter runs in whatever [Zone] was current at the time that the
208-
/// bindings were initialized.
209-
///
210-
/// [`runApp`](https://api.flutter.dev/flutter/widgets/runApp.html) calls
211-
/// [`WidgetsFlutterBinding.ensureInitialized`](https://api.flutter.dev/flutter/widgets/WidgetsFlutterBinding/ensureInitialized.html)
212-
/// so the easiest approach is to call that in [body]:
213-
/// ```
214-
/// void main() {
215-
/// var clientFactory = Client.new; // Constructs the default client.
216-
/// if (Platform.isAndroid) {
217-
/// clientFactory = MyAndroidHttpClient.new;
218-
/// }
219-
/// runWithClient(() => runApp(const MyApp()), clientFactory);
220-
/// }
221-
/// ```
222-
///
223205
/// If [runWithClient] is used and the environment defines
224206
/// `no_default_http_client=true` then generated binaries may be smaller e.g.
225207
/// ```shell
226-
/// $ flutter build appbundle --dart-define=no_default_http_client=true ...
227208
/// $ dart compile exe --define=no_default_http_client=true ...
228209
/// ```
229210
///
230211
/// If `no_default_http_client=true` is set then any call to the [Client]
231212
/// factory (i.e. `Client()`) outside of the [Zone] created by [runWithClient]
232213
/// will throw [StateError].
214+
///
215+
/// > [!IMPORTANT]
216+
/// > Flutter does not guarantee that callbacks are executed in a particular
217+
/// > [Zone].
218+
/// >
219+
/// > Instead of using [runWithClient], Flutter developers can use a framework,
220+
/// > such as [`package:provider`](https://pub.dev/packages/provider), to make
221+
/// > a [Client] available throughout their applications.
222+
/// >
223+
/// > See the
224+
/// > [Flutter Http Example](https://github.com/dart-lang/http/tree/master/pkgs/flutter_http_example).
233225
R runWithClient<R>(R Function() body, Client Function() clientFactory,
234226
{ZoneSpecification? zoneSpecification}) =>
235227
runZoned(body,

0 commit comments

Comments
 (0)