@@ -113,6 +113,87 @@ the [`RetryClient()`][new RetryClient] constructor.
113113
114114[ new RetryClient ] : https://pub.dev/documentation/http/latest/retry/RetryClient/RetryClient.html
115115
116+ ## Aborting requests
117+
118+ Some clients, such as [ ` BrowserClient ` ] [ browserclient ] , [ ` IOClient ` ] [ ioclient ] , and
119+ [ ` RetryClient ` ] [ retryclient ] , support abortion of requests in-flight.
120+
121+ Abortion in this way can only be performed when using [ ` Client.send ` ] [ clientsend ] or
122+ [ ` BaseRequest.send ` ] [ baserequestsend ] with an [ ` Abortable ` ] [ abortable ] request (such
123+ as [ ` AbortableRequest ` ] [ abortablerequest ] ).
124+
125+ To abort a request, complete the [ ` Abortable.abortTrigger ` ] [ aborttrigger ] .
126+
127+ Depending on when the abortion is triggered, an [ ` AbortedRequest ` ] [ abortedrequest ] may be thrown in different places.
128+
129+ ``` dart
130+ import 'dart:async';
131+ import 'package:http/http.dart' as http;
132+
133+ Future<void> main() async {
134+ final abortTrigger = Completer<void>();
135+ final client = Client();
136+ final request = AbortableRequest(
137+ 'GET',
138+ Uri.parse('http://example.org'),
139+ abortTrigger: abortTrigger.future,
140+ );
141+
142+ // Whenever abortion is required:
143+ // > abortTrigger.complete();
144+
145+ // Send request
146+ final StreamedResponse response;
147+ try {
148+ response = await client.send(request);
149+ } on AbortedRequest {
150+ // request aborted before it was fully sent
151+ rethrow;
152+ }
153+
154+ // Using full response bytes listener
155+ response.stream.listen(
156+ (data) {
157+ // consume response bytes
158+ },
159+ onError: (Object err) {
160+ if (err is AbortedRequest) {
161+ // request aborted whilst response bytes are being streamed;
162+ // the stream will always be finished early
163+ }
164+ },
165+ onDone: () {
166+ // response bytes consumed, or partially consumed if finished
167+ // early due to abortion
168+ },
169+ );
170+
171+ // Alternatively, using `asFuture`
172+ try {
173+ await response.stream.listen(
174+ (data) {
175+ // consume response bytes
176+ },
177+ ).asFuture<void>();
178+ } on AbortedRequest {
179+ // request aborted whilst response bytes are being streamed
180+ rethrow;
181+ }
182+ // response bytes fully consumed
183+ }
184+ ```
185+
186+ [ browserclient ] : https://pub.dev/documentation/http/latest/browser_client/BrowserClient-class.html
187+ [ ioclient ] : https://pub.dev/documentation/http/latest/io_client/IOClient-class.html
188+ [ retryclient ] : https://pub.dev/documentation/http/latest/retry/RetryClient-class.html
189+ [ clientsend ] : https://pub.dev/documentation/http/latest/http/Client/send.html
190+ [ baserequestsend ] : https://pub.dev/documentation/http/latest/http/BaseRequest/send.html
191+ [ abortable ] : https://pub.dev/documentation/http/latest/http/Abortable-class.html
192+ [ abortablerequest ] : https://pub.dev/documentation/http/latest/http/AbortableRequest-class.html
193+ [ aborttrigger ] : https://pub.dev/documentation/http/latest/http/Abortable/abortTrigger.html
194+ [ abortedrequest ] : https://pub.dev/documentation/http/latest/http/AbortedRequest-class.html
195+
196+
116197## Choosing an implementation
117198
118199There are multiple implementations of the ` package:http ` [ ` Client ` ] [ client ] interface. By default, ` package:http ` uses [ ` BrowserClient ` ] [ browserclient ] on the web and [ ` IOClient ` ] [ ioclient ] on all other platforms. You can choose a different [ ` Client ` ] [ client ] implementation based on the needs of your application.
0 commit comments