runWithClient<R> function

R runWithClient<R>(
  1. R body(),
  2. Client clientFactory(), {
  3. ZoneSpecification? zoneSpecification,
})

Runs body in its own Zone with the Client returned by clientFactory set as the default Client.

For example:

class MyAndroidHttpClient extends BaseClient {
  @override
  Future<http.StreamedResponse> send(http.BaseRequest request) {
    // your implementation here
  }
}

void main() {
  Client client =  Platform.isAndroid ? MyAndroidHttpClient() : Client();
  runWithClient(myFunction, () => client);
}

void myFunction() {
  // Uses the `Client` configured in `main`.
  final response = await get(Uri.https('www.example.com', ''));
  final client = Client();
}

The Client returned by clientFactory is used by the Client.new factory and the convenience HTTP functions (e.g. http.get)

Implementation

R runWithClient<R>(R Function() body, Client Function() clientFactory,
        {ZoneSpecification? zoneSpecification}) =>
    runZoned(body,
        zoneValues: {#_clientToken: Zone.current.bindCallback(clientFactory)},
        zoneSpecification: zoneSpecification);