fix(chat): propagate empty draft on target swap and dispose
Oracle re-review on PR #30 flagged that _ChatDetailViewState only called onDraftChanged when _textCtl.text was non-empty. The empty case is load-bearing: if the user restored a saved draft, deleted the text, then switched target (or closed the panel), the parent's draft map kept the stale entry and resurrected it on the next swap. Fix: call onDraftChanged unconditionally in both didUpdateWidget (target change) and dispose (tear-down), so the parent map learns when a draft is now empty. Adds a regression test exercising the restore-clear-swap sequence.
This commit is contained in:
@@ -1144,9 +1144,12 @@ class _ChatDetailViewState extends State<ChatDetailView> {
|
||||
void didUpdateWidget(covariant ChatDetailView oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.target != widget.target) {
|
||||
if (oldWidget.onDraftChanged != null && _textCtl.text.isNotEmpty) {
|
||||
oldWidget.onDraftChanged!(_textCtl.text);
|
||||
}
|
||||
// Propagate the OUTGOING draft unconditionally, including empty
|
||||
// text. The empty case is load-bearing: if the user typed text,
|
||||
// saved it, restored it, then deleted everything, the parent
|
||||
// map must learn the draft is now empty — otherwise the stale
|
||||
// entry resurrects on the next target swap.
|
||||
oldWidget.onDraftChanged?.call(_textCtl.text);
|
||||
_textCtl.text = widget.restoredDraft ?? '';
|
||||
_lastRenderedTarget = null;
|
||||
}
|
||||
@@ -1154,9 +1157,10 @@ class _ChatDetailViewState extends State<ChatDetailView> {
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
if (widget.onDraftChanged != null && _textCtl.text.isNotEmpty) {
|
||||
widget.onDraftChanged!(_textCtl.text);
|
||||
}
|
||||
// Same unconditional flush on tear-down. The `isNotEmpty` guard
|
||||
// here would silently drop "user cleared the field then closed
|
||||
// the panel" into the same stale-entry bug class as didUpdateWidget.
|
||||
widget.onDraftChanged?.call(_textCtl.text);
|
||||
_textCtl.dispose();
|
||||
_scrollCtl.dispose();
|
||||
super.dispose();
|
||||
|
||||
Reference in New Issue
Block a user