I’m working on a Flutter app and I have a function that performs several tasks after logging in, including popping the current context and then showing a dialog. However, I’m running into an issue where the dialog does not appear because the context has already been popped.
Code:
Future<void> _confirmLoginPassword() async {
await _downloadAll();
HolupUserProvider _userProvider = Provider.of<HolupUserProvider>(
context, listen: false);
HolupReleaseInfoProvider _releaseProvider =
Provider.of<HolupReleaseInfoProvider>(context, listen: false);
String? _lastSeenRelease = DateTime.now().toIso8601StringFormatted();
double height = MediaQuery
.of(context)
.size
.height;
if (mounted) {
List<HolupReleaseInfo>? releaseFeatures = _releaseProvider.findNewerReleases(
_userProvider.profile?.lastSeenRelease ?? DateTime.now());
Navigator.of(context).pop();
showHolupToast("Si prihlásený");
if (releaseFeatures == null) {
_downloadNewReleaseInfo(_releaseProvider);
}
if (releaseFeatures != null && releaseFeatures.isNotEmpty) {
await Future.delayed(Duration(seconds: 3), () {
showHolupBottomDialog(
context,
HolupBottomSheet(
contentPadding: EdgeInsets.symmetric(
vertical: 24, horizontal: 205),
title: "Novinky v Holupovi",
child: ReleaseInfoList(
showLatestReleaseOnly: false,
),
height: height - 64,
),
);
NewReleaseApi.updateLastSeenRelease(
_userProvider.baseNumber!, _lastSeenRelease,
_userProvider.token!);
_releaseProvider.show = false;
});
}
}
}
The Issue:
When I call Navigator.of(context).pop(), it pops the current context, but then when I try to show the dialog immediately afterward, I get an error because the context has been unmounted.
Error:
This widget has been unmounted, so the State no longer has a context (and should be considered defunct).
Consider canceling any active work during “dispose” or using the “mounted” getter to determine if the State is still active.
What I Need:
I need a reliable way to show a dialog immediately after popping the current context in the same function. How can I achieve this? Or any other way how to show dialog after some function, not dependant on context.