Commit 4558737
committed
fix(message_widget): guard MessageCard._updateWidthLimit against unlaid RenderBox
`_MessageCardState._updateWidthLimit` is scheduled via
`WidgetsBinding.instance.addPostFrameCallback` from `didChangeDependencies`,
and reads `renderBox.size.width` when it fires. If the attachments subtree
has been detached from the tree between scheduling and firing (e.g. the
parent removed the message because the list was rebuilt, the chat was
disposed, or the attachment was deleted), `RenderBox.size` throws
`StateError: RenderBox was not laid out` per the framework contract.
The throw is caught by `FlutterError.onError`, so users see a dropped frame
rather than a crash — but Sentry still records it as a fatal error. In one
production Flutter app the two corresponding Sentry issues have generated
1500+ events across 1100+ unique users in a single 2-week release. The error
appears under any view path that has chat messages with attachments (chat,
groupPost, splash, root).
Fix is a `hasSize` guard before reading `.size`, plus moving the existing
`mounted` check to the top of the function for symmetry (it was previously
only checked before `setState`, so we still touched the unmounted render
object first).
The render-box-was-not-laid-out error is the canonical signature of this
mistake in Flutter; the `hasSize` check is exactly what the framework docs
recommend for this case.1 parent 7f0804d commit 4558737
1 file changed
Lines changed: 9 additions & 5 deletions
Lines changed: 9 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
133 | 133 | | |
134 | 134 | | |
135 | 135 | | |
| 136 | + | |
| 137 | + | |
136 | 138 | | |
137 | 139 | | |
138 | | - | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
139 | 145 | | |
140 | | - | |
| 146 | + | |
141 | 147 | | |
142 | | - | |
143 | | - | |
144 | | - | |
| 148 | + | |
145 | 149 | | |
146 | 150 | | |
147 | 151 | | |
| |||
0 commit comments