runWithClient<R> function
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() {
var clientFactory = Client.new; // Constructs the default client.
if (Platform.isAndroid) {
clientFactory = MyAndroidHttpClient.new;
}
runWithClient(myFunction, clientFactory);
}
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). If clientFactory
returns Client() then the default Client is used.
Implementation
R runWithClient<R>(R Function() body, Client Function() clientFactory,
{ZoneSpecification? zoneSpecification}) =>
runZoned(body,
zoneValues: {#_clientToken: Zone.current.bindCallback(clientFactory)},
zoneSpecification: zoneSpecification);