I want to cancel some page loading (request to APIs) only when user tapped back button or swiped back to the original page.
For example, when user in the Home tab and tap Article tab, the app will request API to get list of articles or error message (show popup) when it fails to get those.
But currently, the request will not be canceled even if user go back to Home tab while requesting so the error message pops up on the Home tab.
Is there any way I can safely cancel initialize function as below?
Article tab
Future<void> initialize() async {
WidgetsBinding.instance!.addObserver(this);
Future.delayed(const Duration(milliseconds: 250), () {
_loadArticleContents();
});
}
Future<void> _loadArticleContents() async {
try {
final result = await _apiClient.sendRequest(
MatomeArticleRequest(id: _identity.id),
);
if (result.item2 != null) {
// I want to cancel this to be shown when user go back to some other tabs
LoadErrorDialog.show(
onTap: () => _navigator.pop(),
);
return;
}
} catch (e) {
Log.debug(e);
LoadErrorDialog.show(
onTap: () => _navigator.pop(),
);
}
}