@@ -113,6 +113,92 @@ the [`RetryClient()`][new RetryClient] constructor.
113
113
114
114
[ new RetryClient ] : https://pub.dev/documentation/http/latest/retry/RetryClient/RetryClient.html
115
115
116
+ ## Aborting requests
117
+
118
+ Some clients, such as [ ` BrowserClient ` ] [ browserclient ] , [ ` IOClient ` ] [ ioclient ] , and
119
+ [ ` RetryClient ` ] [ retryclient ] , support aborting requests before they complete.
120
+
121
+ Aborting 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 ] ` Future ` .
126
+
127
+ If the request is aborted before the response ` Future ` completes, then the response
128
+ ` Future ` will complete with [ ` RequestAbortedException ` ] [ requestabortedexception ] . If
129
+ the response is a ` StreamedResponse ` and the the request is cancelled while the
130
+ response stream is being consumed, then the response stream will contain a
131
+ [ ` RequestAbortedException ` ] [ requestabortedexception ] .
132
+
133
+ ``` dart
134
+ import 'dart:async';
135
+
136
+ import 'package:http/http.dart' as http;
137
+
138
+ Future<void> main() async {
139
+ final abortTrigger = Completer<void>();
140
+ final client = Client();
141
+ final request = AbortableRequest(
142
+ 'GET',
143
+ Uri.https('example.com'),
144
+ abortTrigger: abortTrigger.future,
145
+ );
146
+
147
+ // Whenever abortion is required:
148
+ // > abortTrigger.complete();
149
+
150
+ // Send request
151
+ final StreamedResponse response;
152
+ try {
153
+ response = await client.send(request);
154
+ } on RequestAbortedException {
155
+ // request aborted before it was fully sent
156
+ rethrow;
157
+ }
158
+
159
+ // Using full response bytes listener
160
+ response.stream.listen(
161
+ (data) {
162
+ // consume response bytes
163
+ },
164
+ onError: (Object err) {
165
+ if (err is RequestAbortedException) {
166
+ // request aborted whilst response bytes are being streamed;
167
+ // the stream will always be finished early
168
+ }
169
+ },
170
+ onDone: () {
171
+ // response bytes consumed, or partially consumed if finished
172
+ // early due to abortion
173
+ },
174
+ );
175
+
176
+ // Alternatively, using `asFuture`
177
+ try {
178
+ await response.stream.listen(
179
+ (data) {
180
+ // consume response bytes
181
+ },
182
+ ).asFuture<void>();
183
+ } on RequestAbortedException {
184
+ // request aborted whilst response bytes are being streamed
185
+ rethrow;
186
+ }
187
+ // response bytes fully consumed
188
+ }
189
+ ```
190
+
191
+ [ browserclient ] : https://pub.dev/documentation/http/latest/browser_client/BrowserClient-class.html
192
+ [ ioclient ] : https://pub.dev/documentation/http/latest/io_client/IOClient-class.html
193
+ [ retryclient ] : https://pub.dev/documentation/http/latest/retry/RetryClient-class.html
194
+ [ clientsend ] : https://pub.dev/documentation/http/latest/http/Client/send.html
195
+ [ baserequestsend ] : https://pub.dev/documentation/http/latest/http/BaseRequest/send.html
196
+ [ abortable ] : https://pub.dev/documentation/http/latest/http/Abortable-class.html
197
+ [ abortablerequest ] : https://pub.dev/documentation/http/latest/http/AbortableRequest-class.html
198
+ [ aborttrigger ] : https://pub.dev/documentation/http/latest/http/Abortable/abortTrigger.html
199
+ [ requestabortedexception ] : https://pub.dev/documentation/http/latest/http/RequestAbortedException-class.html
200
+
201
+
116
202
## Choosing an implementation
117
203
118
204
There 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