Skip to content

fix: guard BuildContext usage across async gaps in task, ticket, and workspace screens#226

Open
Muneerali199 wants to merge 1 commit intoAOSSIE-Org:mainfrom
Muneerali199:fix/buildcontext-async-gap-screens
Open

fix: guard BuildContext usage across async gaps in task, ticket, and workspace screens#226
Muneerali199 wants to merge 1 commit intoAOSSIE-Org:mainfrom
Muneerali199:fix/buildcontext-async-gap-screens

Conversation

@Muneerali199
Copy link
Copy Markdown

@Muneerali199 Muneerali199 commented Mar 13, 2026

Summary

Fixes #223

Resolves use_build_context_synchronously lint warnings in three screens where ScaffoldMessenger.of(context) was called after an await without a mounted guard.

Problem

After an await (e.g., await Navigator.push(...)), the widget may have been disposed. Calling ScaffoldMessenger.of(context) on a stale BuildContext can cause a runtime crash:

FlutterError: Looking up a deactivated widget's ancestor is unsafe.

The analyzer correctly flagged these with use_build_context_synchronously.

Files Changed

File Issue
lib/screens/tasks/task_screen.dart ScaffoldMessenger.of(context) after await Navigator.push in FAB onPressed
lib/screens/tickets/ticket_screen.dart Same pattern in FAB onPressed
lib/screens/workspace/workspace_screen.dart Three .then((result) async {}) callbacks after await calls

Fix Applied

Two complementary techniques used per Flutter best practices:

  1. Pre-capture the messenger before the await:

    final messenger = ScaffoldMessenger.of(context);
    final result = await Navigator.push(...);
    if (result == true && mounted) {
      messenger.showSnackBar(...); // safe: captured before await
    }
  2. mounted guard in .then() callbacks (workspace screen):

    .then((result) async {
      if (result == true) {
        await supabaseService.loadTeamMembers(...);
        if (mounted) {
          ScaffoldMessenger.of(context).showSnackBar(...);
        }
      }
    });

Before / After

Before (flutter analyze):

info - Don't use 'BuildContext's across async gaps - task_screen.dart:178 - use_build_context_synchronously
info - Don't use 'BuildContext's across async gaps - ticket_screen.dart:165 - use_build_context_synchronously
info - Don't use 'BuildContext's across async gaps - workspace_screen.dart:83 - use_build_context_synchronously
info - Don't use 'BuildContext's across async gaps - workspace_screen.dart:109 - use_build_context_synchronously
info - Don't use 'BuildContext's across async gaps - workspace_screen.dart:135 - use_build_context_synchronously

After (flutter analyze lib/screens/tasks/task_screen.dart lib/screens/tickets/ticket_screen.dart lib/screens/workspace/workspace_screen.dart):

23 issues found.

Zero use_build_context_synchronously warnings remaining in these files.

Checklist

  • flutter analyze run — no new warnings introduced
  • All use_build_context_synchronously warnings in the three files resolved
  • Fix follows Flutter official guidance (pre-capture or mounted check)
  • No logic changes — only safety guards added

Summary by CodeRabbit

  • Bug Fixes
    • Fixed potential crashes when displaying success or error notifications after navigating away from screens.
    • Improved stability when creating tasks, tickets, or meetings by ensuring notifications only display when screens are active.

…ce screens

Wrap ScaffoldMessenger.of(context) calls with mounted checks and
pre-capture messenger before await to satisfy use_build_context_synchronously
lint rule in task_screen.dart, ticket_screen.dart, and workspace_screen.dart.

Fixes AOSSIE-Org#223
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 13, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 9dd44177-622c-465f-9cdf-6ed17135d614

📥 Commits

Reviewing files that changed from the base of the PR and between 5afe656 and b9ef95b.

📒 Files selected for processing (3)
  • lib/screens/tasks/task_screen.dart
  • lib/screens/tickets/ticket_screen.dart
  • lib/screens/workspace/workspace_screen.dart

Walkthrough

This PR fixes a critical bug where BuildContext is accessed across async gaps without verifying widget mounting, causing "Looking up a deactivated widget's ancestor is unsafe" crashes during navigation. The fix adds mounted checks before showing SnackBars and other UI feedback in task, ticket, and workspace screens.

Changes

Cohort / File(s) Summary
BuildContext Safety Fixes
lib/screens/tasks/task_screen.dart, lib/screens/tickets/ticket_screen.dart, lib/screens/workspace/workspace_screen.dart
Added mounted guards before accessing context in error handlers, success callbacks, and post-async operations. Introduced local ScaffoldMessenger instances captured before async calls to ensure safe UI feedback delivery when widgets remain mounted.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested labels

Dart/Flutter

Poem

🐰 A widget unmounted, context alas—
We caught the crash before it could pass!
With mounted checks standing guard so true,
Our SnackBars now know what to do.

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the primary fix: guarding BuildContext usage across async gaps with mounted checks and pre-captured messenger instances in three screen files.
Linked Issues check ✅ Passed All coding requirements from issue #223 are met: seven instances of unsafe BuildContext usage after async operations have been addressed with mounted guards or pre-captured messenger instances across the three specified files.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the BuildContext safety issue in the three specified files; no unrelated modifications or logic changes were introduced.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

BUG: BuildContext used across async gaps causes crash in task, ticket and workspace screens

1 participant