fix(mobile): skip the emoji picker when its opener is gone (#9820)

On iOS, opening the emoji picker from a context that had already been
unmounted threw `StateError('No ProviderScope found')` from
`ProviderScope.containerOf`. It threw after the picker had taken its
one-sheet-at-a-time guard, so every later emoji picker open in that
session was silently refused until the app restarted.

The message actions sheet hits this: its "+" tile opens the picker on
the long-pressed message row's context, which the timeline can rebuild
away while the sheet is open (the reported crash came from a phone in
landscape, tapping "+" in the quick reaction row).

`showEmojiPicker` now returns early, completing the caller's
`onDismiss`, when its context is no longer mounted, and the guard is
never taken. That tap still opens no picker (the message usually stays
on screen, but its row was rebuilt), so the user has to retry, but later
opens keep working.

### Validation

- Added [an iOS picker
test](https://github.com/block/buzz/blob/fix-ios-emoji-picker-dead-context/mobile/test/features/channels/emoji_picker_test.dart)
that opens the picker from a rebuilt-away context, then checks the
caller's dismissal runs and a later open from a live context still
presents the native sheet.
- Ran the fixed build in an iPhone 17 Pro simulator against a live
community: long-pressed a message, tapped "+" in the quick reaction row,
picked an emoji from the native sheet and saw the reaction land; then
repeated on a second message to confirm the picker opens again (with the
first pick under Frequently used). The simulator can't reproduce the
rebuilt-row race itself; the added test covers that.

### Regressions

None expected. The only behavior change is for an opener whose widget is
already gone, which previously threw and blocked the picker. Mounted
callers take exactly the same path as before.

Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
This commit is contained in:
Tom Brow
2026-11-20 12:02:49 -08:00
committed by GitHub
parent eae69483fd
commit 51b0ea62be
2 changed files with 92 additions and 0 deletions

View File

@@ -43,6 +43,14 @@ void showEmojiPicker({
VoidCallback? onDismiss,
bool nativeEmojiOnly = false,
}) {
// A caller can open the picker from a context that outlived its widget, such
// as the message row behind the actions sheet after the timeline rebuilt it.
// There is nothing left to present on or react for, so complete the caller's
// lifecycle instead of throwing from the ancestor lookups below.
if (!context.mounted) {
onDismiss?.call();
return;
}
if (defaultTargetPlatform == TargetPlatform.iOS) {
unawaited(
_presentIosEmojiPicker(

View File

@@ -1186,6 +1186,90 @@ void main() {
},
);
testWidgets(
'an open from an unmounted context does not block later opens',
(tester) async {
final previousPlatform = debugDefaultTargetPlatformOverride;
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
final prefs = await _prefs();
var presents = 0;
_setMockNativeEmojiPickerHandler((call) async {
if (call.method == 'present') presents += 1;
return true;
});
final showStaleRow = ValueNotifier(true);
late BuildContext staleContext;
var staleDismissals = 0;
try {
await tester.pumpWidget(
ProviderScope(
overrides: [
savedPrefsProvider.overrideWithValue(prefs),
myPubkeyProvider.overrideWithValue('self'),
customEmojiPaletteProvider.overrideWith(
() => _FakeCustomEmojiPaletteNotifier(
Future.value(_customEmoji),
),
),
],
child: MaterialApp(
theme: AppTheme.light(),
home: Scaffold(
body: ValueListenableBuilder<bool>(
valueListenable: showStaleRow,
builder: (context, show, _) => Column(
children: [
if (show)
Builder(
builder: (rowContext) {
staleContext = rowContext;
return const SizedBox.shrink();
},
),
FilledButton(
onPressed: () => showEmojiPicker(
context: context,
onSelect: (_) {},
),
child: const Text('Open live'),
),
],
),
),
),
),
),
);
// The message row an actions sheet captured is rebuilt away before
// its "+" opens the picker.
showStaleRow.value = false;
await tester.pumpAndSettle();
expect(staleContext.mounted, isFalse);
showEmojiPicker(
context: staleContext,
onSelect: (_) {},
onDismiss: () => staleDismissals += 1,
);
await tester.pumpAndSettle();
expect(staleDismissals, 1);
expect(presents, 0);
await tester.tap(find.text('Open live'));
await tester.pumpAndSettle();
expect(presents, 1);
await _sendNativeEmojiPickerCall(tester, 'dismissed');
} finally {
_setMockNativeEmojiPickerHandler(null);
debugDefaultTargetPlatformOverride = previousPlatform;
showStaleRow.dispose();
}
},
);
testWidgets('a reentrant open cannot steal the live sheet callbacks', (
tester,
) async {