Fix ImageStream reporting errors globally when no active listeners exist #180327
+137
−37
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.
fix #81931
Run the following app and quickly tap the FAB multiple times to output an exception to the console. However,
N/Acontinues to be displayed on the UI, anderrorBuilderis functioning correctly.Root Cause Analysis
I investigated why the error is reported even after the widget is disposed. The main reason is that
ImageCachekeeps listening to the stream even after theImagewidget is disposed.Although the
ImageCachelistener is "passive" (it has noonErrorcallback), the current implementation mistakenly treats its presence as "someone is listening." Since no one actually handles the error, it reports the error globally toFlutterError.onError. In essence, the system mistakes the cache listener for an active error handler.The Fix
I added a check to see if there are any active listeners (listeners with an
onErrorcallback). If only passive listeners remain (likeImageCache), the error will now be silenced instead of reported globally. This effectively solves the issue of "abandoned" streams reporting errors after disposal.Discussion
This change slightly updates the behavior in Release Mode when no
errorBuilderis used. Previously, errors were reported toFlutterError.onError, but now they will be silent.I believe this change is correct and necessary for the following reasons:
errorBuilder, it implies they do not explicitly support or handle error cases for that widget.errorBuilder, reporting these exceptions toFlutterError.onErroris logically incorrect.FlutterError.onErroris intended for unhandled exceptions, whereas these failures represent handled UI states, not application crashes.Pre-launch Checklist
///).If you need help, consider asking for advice on the #hackers-new channel on Discord.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assistbot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.