Skip to content

Conversation

@krushnarout
Copy link
Member

@krushnarout krushnarout commented Dec 29, 2025

final conversationProvider = Provider.of<ConversationProvider>(context, listen: false);

here the provider was accessed inside a post-frame async callback. During that time, the widget was still mounted but its Provider was no longer in the widget tree, so the provider lookup returned null

closes #3949

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces if (!mounted) return; checks within the initState method's addPostFrameCallback in ConversationsPage. These additions are crucial for preventing potential runtime errors that can occur when asynchronous operations complete after the widget has been disposed, leading to attempts to access a stale BuildContext or call setState on a disposed widget. This is a good practice for ensuring the stability and correctness of the application.

@override
void initState() {
WidgetsBinding.instance.addPostFrameCallback((_) async {
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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FlutterError - Null check operator used on a null value - _ConversationsPageState.initState.<fn>

2 participants