Skip to content

feat(GetX): update controller on tag change and add corresponding test#3441

Open
Danilospano00 wants to merge 1 commit intojonataslaw:masterfrom
Danilospano00:fix/getx-widget-tag-update
Open

feat(GetX): update controller on tag change and add corresponding test#3441
Danilospano00 wants to merge 1 commit intojonataslaw:masterfrom
Danilospano00:fix/getx-widget-tag-update

Conversation

@Danilospano00
Copy link

Fix: Handle tag changes in GetX widget lifecycle

Description

This PR fixes a bug in the GetX widget where changes to the tag parameter during widget updates were not properly handled.

Before: When the tag parameter changed in didUpdateWidget, the widget would continue using the controller associated with the old tag, causing inconsistent behavior and potential memory leaks.

After: The widget now properly detects tag changes and handles the transition by cleaning up the old controller and re-initializing with the new tag.

Changes Made

Modified: lib/get_state_manager/src/rx_flutter/rx_getx_widget.dart

  • Added logic in didUpdateWidget to detect when the tag parameter changes
  • Properly clean up the old controller if it was created by this widget and autoRemove is enabled
  • Clear all existing disposers to prevent memory leaks
  • Re-initialize the controller with the new tag, following the same logic as initState:
    • For global controllers: find existing or create new
    • For local controllers: create new instance

Added: test/state_manager/getx_widget_tag_update_test.dart

  • Test case that verifies the widget correctly switches controllers when the tag changes
  • Creates two controllers with different tags and values
  • Verifies that rebuilding the widget with a different tag displays data from the correct controller

Why This Change Is Needed

When building dynamic UIs where the same GetX widget needs to switch between different controller instances (e.g., in a list or tabs), the tag parameter allows selecting which controller to use. Without this fix, changing the tag would not update which controller the widget listens to, breaking reactivity and potentially causing memory leaks from undisposed subscriptions.

This brings the didUpdateWidget lifecycle method in line with the initialization logic, ensuring consistent behavior throughout the widget's lifecycle.

Pre-launch Checklist

  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making or feature I am adding, or @jonataslaw said the PR is test-exempt.
  • All existing and new tests are passing.

SteveHawes added a commit to SteveHawes/getx that referenced this pull request Feb 25, 2026
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.

1 participant