close method
Closes the client and cleans up any resources associated with it.
It's important to close each client when it's done being used; failing to do so can cause the Dart process to hang.
Once close is called, no other methods should be called. If close is called while other asynchronous methods are running, the behavior is undefined.
When force
is true
(the default), the WrapperClient might
close the inner client. To avoid closing the inner client, set force
to true.
The optional keepAliveHttpClient
parameter is used to specify an HTTP Client
that should not be closed by this close method. This can be useful for
HTTP clients that are used to make long-lived connections to remote
servers. If it is not specified, all internal HTTP clients will be closed
by this close method when force
is true
.
Note: force
is true
by default to keep this close's default
behaviour consistent with Client.close
because BaseClient.close
doesn't have a parameter to avoid closing its inner client.
Implementation
@override
@mustCallSuper
/// Closes the client and cleans up any resources associated with it.
///
/// It's important to close each client when it's done being used; failing to
/// do so can cause the Dart process to hang.
///
/// Once [close] is called, no other methods should be called. If [close] is
/// called while other asynchronous methods are running, the behavior is
/// undefined.
///
/// When [force] is `true` (the default), the [WrapperClient] might
/// close the inner client. To avoid closing the inner client, set [force] to true.
///
/// The optional [keepAliveHttpClient] parameter is used to specify an HTTP [Client]
/// that should not be closed by this [close] method. This can be useful for
/// HTTP clients that are used to make long-lived connections to remote
/// servers. If it is not specified, all internal HTTP clients will be closed
/// by this [close] method when [force] is `true`.
///
/// Note: [force] is `true` by default to keep this [close]'s default
/// behaviour consistent with [Client.close] because [BaseClient.close]
/// doesn't have a parameter to avoid closing its inner client.
void close({
bool force = true,
Client? keepAliveHttpClient,
}) {
if (!force) return;
final client = _inner;
if (client == null || client == keepAliveHttpClient) return;
if (client is WrapperClient) {
client.close(force: force, keepAliveHttpClient: keepAliveHttpClient);
} else {
client.close();
}
_inner = null;
}