Skip to content

🐛 Check modal open state before calling Navigator.pop when closing#332

Open
AlexV525 wants to merge 2 commits intoreown-com:developfrom
AlexV525:fix/modal-close-check
Open

🐛 Check modal open state before calling Navigator.pop when closing#332
AlexV525 wants to merge 2 commits intoreown-com:developfrom
AlexV525:fix/modal-close-check

Conversation

@AlexV525
Copy link
Copy Markdown
Contributor

Description

Adding _isOpen check when calling Navigator.pop in closeModal to avoid unnecessary navigation changes.

Resolves #331

Copilot AI review requested due to automatic review settings January 27, 2026 14:03
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to prevent Navigator.pop() from being called when the AppKit modal was never opened, addressing issue #331.

Changes:

  • Add an _isOpen guard before attempting to Navigator.pop() in closeModal.
Comments suppressed due to low confidence (1)

packages/reown_appkit/lib/modal/appkit_modal_impl.dart:1411

  • closeModal calls _close() before checking _isOpen. Since _close() sets _isOpen = false when the modal was open, the new guard if (_isOpen && _context != null) will always be false in the open case, so Navigator.pop() will never run and the modal route may remain on the stack. Capture the previous open state (e.g., final wasOpen = _isOpen;) before _close(), or move the pop logic into _close() before toggling _isOpen so you only pop when it was actually open.
    // If we aren't open, then we can't and shouldn't close
    _close();
    if (_isOpen && _context != null) {
      final canPop = Navigator.of(_context!, rootNavigator: true).canPop();
      if (canPop) {
        Navigator.of(_context!, rootNavigator: true).pop();
      }
    }

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@AlexV525 AlexV525 force-pushed the fix/modal-close-check branch 2 times, most recently from ebc18dc to b79103d Compare January 27, 2026 14:42
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

if (canPop) {
Navigator.of(_context!, rootNavigator: true).pop();
}
Navigator.maybeOf(_context!, rootNavigator: true)?.maybePop();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey @AlexV525 👋 do we need this change?

closeModal should close the modal unconditionally, and maybePop could refuse to close it in some cases, right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a regular practise navigation op for packages with my experience. Using maybePop will call predicate normally if users have customized the Navigator.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @AlexV525 sorry the delay 🙏

Agreed it’s good practice for general navigation, but _close() mutates internal state (sets _isOpen = false, clears widget stack) before the pop. If maybePop gets vetoed, the navigator and SDK state diverge — the modal is still on screen but the SDK considers it closed, and it won’t retry. Since this is our own programmatic dismiss, canPop() + pop() keeps things consistent.

Happy to keep just the !_isOpen guard — that’s the real fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[appkit] Navigator.pop is called without properly checking if the modal has been opened

3 participants