cancelRequest method
Cancels in-flight requests — every one of them, or just those made under
a tag.
Call this when leaving a screen to abort any pending requests that are
no longer needed (e.g. in onClose / dispose).
Cancel everything (omit tag):
@override
void onClose() {
_api.cancelRequest(cancellationReason: 'Screen closed');
super.onClose();
}
Cancel one screen's requests. Without tag this is all-or-nothing,
so a screen tearing down would abort requests belonging to screens still
on the stack. Pass the same tag to the requests and to the cancel:
await _api.get(endpoint: '/feed', tag: 'feed');
await _api.get(endpoint: '/stories', tag: 'feed');
_api.cancelRequest(tag: 'feed', cancellationReason: 'Left the feed');
Untagged requests are cancelled only by a call that omits tag.
Implementation
@override
void cancelRequest({String? tag, String cancellationReason = ''}) {
cancellations.add(cancellationReason);
cancelledTags.add(tag);
}