Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions app/lib/pages/conversations/conversations_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,18 @@ class _ConversationsPageState extends State<ConversationsPage> with AutomaticKee

@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) async {
final conversationProvider = Provider.of<ConversationProvider>(context, listen: false);
if (!mounted) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Adding if (!mounted) return; here is a critical improvement. In asynchronous initState callbacks, especially after await calls, the widget might be disposed before the callback resumes. Accessing context or calling setState when mounted is false can lead to runtime errors or crashes. This check correctly guards against such scenarios.

final conversationProvider = context.read<ConversationProvider>();
if (conversationProvider.conversations.isEmpty) {
await conversationProvider.getInitialConversations();
}

if (!mounted) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This second if (!mounted) return; check is also important. Even if the first await call completes successfully, the widget could still be unmounted before the subsequent Provider.of call for folderProvider. Placing this check ensures that context is only accessed if the widget is still active in the widget tree, preventing potential BuildContext related errors.


// Load folders for folder tabs
final folderProvider = Provider.of<FolderProvider>(context, listen: false);
final folderProvider = context.read<FolderProvider>();
if (folderProvider.folders.isEmpty) {
await folderProvider.loadFolders();
}
Expand All @@ -56,7 +60,6 @@ class _ConversationsPageState extends State<ConversationsPage> with AutomaticKee
await _appReviewService.showReviewPromptIfNeeded(context, isProcessingFirstConversation: true);
}
});
super.initState();
}

void scrollToTop() {
Expand Down