Conversation
_listenQuery delays its setState with a microtask so that fetchMore can be called from a child's build. That build can be in the frame that removes the FirestoreQueryBuilder (a list item scrolled away, a route torn down), so the microtask runs after dispose and throws.
There was a problem hiding this comment.
Code Review
This pull request ensures that setState is only called if the widget is still mounted by adding a mounted check inside the delayed Future.microtask callback, and adds a widget test to cover this scenario. The review feedback suggests correcting a typo in the code comment, changing fetchNextpage to fetchMore to align with the public API.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
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.
Description
FirestoreQueryBuilder._listenQuerydelays itssetStatewithFuture.microtaskso thatfetchMorecan be called from a child's build (the documented pattern, e.g. from aListViewitem builder). That build can be in the same frame that removes theFirestoreQueryBuilder— a list item scrolled out of the cache extent, or a route being torn down — so the microtask runs afterdisposeand throws:In release builds this surfaces as
Null check operator used on a null valueinState.setState. We see it in production on web (Flutter wasm), fromquery_builder.dart_listenQuery.This PR checks
mountedin the microtask. Thesnapshots()listener's ownsetStatecalls don't need it, sincedisposecancels that subscription.Test: a
FirestoreQueryBuilderin aListViewwhose builder callsfetchMore; a snapshot arrives and the list is scrolled in the same frame so the item is dropped during layout. It fails without the fix with the error above and passes with it.Related Issues
None found.
Checklist
///).melos run analyze) does not report any problems on my PR.melos run test:unit:alldoesn't fail).Breaking Change