From 51b0ea62be7a05a2e268e00c0e4f3dad1d772d33 Mon Sep 17 00:00:00 2001 From: Tom Brow Date: Fri, 20 Nov 2026 12:02:49 -0800 Subject: [PATCH] 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 Co-authored-by: studio --- mobile/lib/shared/emoji/emoji_picker.dart | 8 ++ .../features/channels/emoji_picker_test.dart | 84 +++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/mobile/lib/shared/emoji/emoji_picker.dart b/mobile/lib/shared/emoji/emoji_picker.dart index 3fffbeca22..cf829366b3 100644 --- a/mobile/lib/shared/emoji/emoji_picker.dart +++ b/mobile/lib/shared/emoji/emoji_picker.dart @@ -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( diff --git a/mobile/test/features/channels/emoji_picker_test.dart b/mobile/test/features/channels/emoji_picker_test.dart index da15cd6161..2d1eba30b0 100644 --- a/mobile/test/features/channels/emoji_picker_test.dart +++ b/mobile/test/features/channels/emoji_picker_test.dart @@ -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( + 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 {