flutter - "Bad state: Future already completed" when handling async login with modal navigation - Stack Overfl

I have two pages in my Flutter app:Settings Page (which has a login button)Login Page (where the user

I have two pages in my Flutter app:

  • Settings Page (which has a login button)
  • Login Page (where the user enters credentials)

Navigation Flow:

  1. On the Settings Page, clicking the login button opens a showModalBottomSheet with useRootNavigator: true.

  2. If the user clicks login inside this modal, I navigate to the Login Page using:

    LoginRoute().push(context);
    
  3. On the Login Page, after entering credentials, the user clicks another login button.

  4. 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 static GlobalKey<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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信