Skip to content

Commit

Permalink
Make it more clear to using use runWithClient in the Dart SDK. (#1250)
Browse files Browse the repository at this point in the history
  • Loading branch information
brianquinlan authored Jun 28, 2024
1 parent 321362a commit 8d89385
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions pkgs/http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,22 @@ void main() {
}
```

In Flutter, you can use a one of many
When using the Flutter SDK, you can use a one of many
[state management approaches][flutterstatemanagement].

If you depend on code that uses top-level functions (e.g. `http.post`) or
calls the [`Client()`][clientconstructor] constructor, then you can use
[`runWithClient`][runwithclient] to ensure that the correct
`Client` is used. When an [Isolate][isolate] is spawned, it does not inherit
any variables from the calling Zone, so `runWithClient` needs to be used in
each Isolate that uses `package:http`.
> [!TIP]
> [The Flutter HTTP example application][flutterhttpexample] demonstrates
> how to make the configured [`Client`][client] available using
> [`package:provider`][provider] and
> [`package:http_image_provider`][http_image_provider].
When using the Dart SDK, you can use [`runWithClient`][runwithclient] to
ensure that the correct [`Client`][client] is used when explicit argument
passing is not an option. For example, if you depend on code that uses
top-level functions (e.g. `http.post`) or calls the
[`Client()`][clientconstructor] constructor. When an [Isolate][isolate] is
spawned, it does not inherit any variables from the calling Zone, so
`runWithClient` needs to be used in each Isolate that uses `package:http`.

You can ensure that only the `Client` that you have explicitly configured is
used by defining `no_default_http_client=true` in the environment. This will
Expand All @@ -269,11 +276,6 @@ $ flutter build appbundle --dart-define=no_default_http_client=true ...
$ dart compile exe --define=no_default_http_client=true ...
```

> [!TIP]
> [The Flutter HTTP example application][flutterhttpexample] demonstrates
> how to make the configured [`Client`][client] available using
> [`package:provider`][provider] and [`package:http_image_provider`][http_image_provider].
[browserclient]: https://pub.dev/documentation/http/latest/browser_client/BrowserClient-class.html
[client]: https://pub.dev/documentation/http/latest/http/Client-class.html
[clientconstructor]: https://pub.dev/documentation/http/latest/http/Client/Client.html
Expand Down

0 comments on commit 8d89385

Please sign in to comment.