fix: dispose auth subscription and TextEditingControllers (closes #173, #174)#175
Merged
X-Wei merged 1 commit intoMay 24, 2026
Conversation
…etworking_rest_api_send_ex Resolves two memory leaks reported in issues X-Wei#173 and X-Wei#174. firebase_login_ex.dart (X-Wei#173): - Store the StreamSubscription returned by _auth.authStateChanges().listen in a `_authSub` field so it can be cancelled. - Override dispose() to cancel the subscription. - Add the missing setState() so the statusText reactsto external auth state changes (token expiration, sign-out from another tab, etc.). - Add a mounted guard before setState() so callbacks that race with navigation do not call setState on a disposed widget. networking_rest_api_send_ex.dart (X-Wei#174): - Override dispose() to release the three TextEditingController instances (_titleController, _contentController, _userIdController) owned by the widget. Without this they were retained for the lifetime of the process every time the user visited this route. No behavior change for the demos beyond the auth listener now actually updating the UI when the auth state changes through the stream.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
This PR is part of an academic analysis of Flutter Catalog conducted for a
mobile development course (ISIS3510, Universidad de los Andes). The focus is
correctness rather than new features. Both changes are confined to the two
files that the linked issues describe; no demo behavior or educational
content is altered beyond what the issues already discussed.
Summary
Closes #173 and #174. Two independent memory-leak fixes:
1.
lib/routes/firebase_login_ex.dart(#173)_auth.authStateChanges().listen(...)previously returned aStreamSubscriptionthat was discarded, and the listener mutated
this._userwithoutsetState,so:
(sign-out from another tab, token expiration, etc.).
The fix:
_authSubfield.dispose()to cancel it.setStatefrom the listener so the displayed status text reacts toexternal auth state changes.
mountedguard beforesetStateso callbacks that race withnavigation do not touch a disposed widget.
2.
lib/routes/networking_rest_api_send_ex.dart(#174)Three
TextEditingControllerinstances were created ininitState()butthe widget never overrode
dispose().TextEditingControlleris aChangeNotifierretained by everyTextFieldthat listens to it. The fixoverrides
dispose()and releases the three controllers.Test plan
git diffreviewed — only the two files are touched, additions only(no behavior removed from the demos).
flutter analyze lib/routes/firebase_login_ex.dart lib/routes/networking_rest_api_send_ex.dart— could not run locally because the project requires Flutter SDK >=3.11
and the local environment is on 3.10. The diff is small and uses only
standard
StatefulWidgetpatterns (no Dart 3.11 syntax).that the status text now updates immediately when auth state changes
through the stream.
_FirebaseLoginExampleStateand_RestApiSendExampleStateinstances are GC'd after navigating away fromeach demo.
Made with Cursor