I have two pages in my Flutter app:
- Settings Page (which has a login button)
- Login Page (where the user enters credentials)
Navigation Flow:
On the Settings Page, clicking the login button opens a
showModalBottomSheet
withuseRootNavigator: true
.If the user clicks login inside this modal, I navigate to the Login Page using:
LoginRoute().push(context);
On the Login Page, after entering credentials, the user clicks another login button.
If login is successful it's calling context.pop() once, but I want to close the loginpage AND close the modal. If login fails, the modal should remain open.
Additional Setup:
My
LoginRoute
is defined with a staticGlobalKey<NavigatorState>
to ensure it is above the bottom navigation bar:static final GlobalKey<NavigatorState> $parentNavigatorKey = rootNavigatorKey;
Issue:
The login button in LoginPage is an async operation (calling a remote API).
However, making the goToLogin button's method in my Modal async results in this error:
Unhandled Exception: Bad state: Future already completed
Stack trace:
E/flutter ( 7668): #0 _AsyncCompleterplete (dart:async/future_impl.dart:84:31) E/flutter ( 7668): #1 ImperativeRouteMatchplete (package:go_router/src/match.dart:456:15) E/flutter ( 7668): #2 GoRouterDelegate._completeRouteMatch (package:go_router/src/delegate.dart:171:14) E/flutter ( 7668): #3 GoRouterDelegate._handlePopPageWithRouteMatch.<anonymous closure> (package:go_router/src/delegate.dart:151:9)
I made the Go to Login button in my modal (AccountPage
) async because I need to await
the result of:
bool? result = await LoginRoute().push<bool>(context);
This allows me to check if the login was successful (true
), and only then close the modal.
However, as soon as I make the button's function async, I encounter the error mentioned above. How can I properly handle this without running into the "Bad state: Future already completed" issue?
If i use WidgetsBinding.instance.addPostFrameCallback((_) {context.pop();}) it does work without any error, however it seems like a strange solution to the problem.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745169986a4614866.html
评论列表(0条)