Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it more clear to using use runWithClient in the Dart SDK. #1250

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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