Skip to content

fix: Guard BuildContext usage across async gaps (use_build_context_synchronously)#202

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

fix: Guard BuildContext usage across async gaps (use_build_context_synchronously)#202
Muneerali199 wants to merge 1 commit intoAOSSIE-Org:mainfrom
Muneerali199:fix/buildcontext-async-gaps

Conversation

@Muneerali199
Copy link
Copy Markdown

@Muneerali199 Muneerali199 commented Mar 13, 2026

Summary

Fixes #197

Resolves 4 use_build_context_synchronously warnings across both apps. Using BuildContext after an await without a proper mounted guard can cause a runtime crash if the widget has been unmounted during the async operation.

Changes

patient/lib/presentation/result/book_appointment.dart

  • Moved if (!mounted) return before the post-await context usage
  • Refactored _selectTimeSlot to use State.context directly instead of accepting a BuildContext parameter, eliminating the stale-context risk entirely

patient/lib/provider/therapist_provider.dart

  • Added if (!context.mounted) return guard before each ScaffoldMessenger.of(context) call that follows await supabase.from(...).insert(...)

therapist/lib/presentation/auth/personal_details_screen.dart

  • Pre-captured Navigator.of(context) and ScaffoldMessenger.of(context) into local variables before the await call
  • Replaced separate per-branch if (mounted) checks with a single if (!mounted) return immediately after the await

Before

flutter analyze patient/
# info - Don't use 'BuildContext's across async gaps - book_appointment.dart:55
# info - Don't use 'BuildContext's across async gaps - therapist_provider.dart:65, 69

flutter analyze therapist/
# info - Don't use 'BuildContext's across async gaps, guarded by an unrelated 'mounted' check
#         - personal_details_screen.dart:115, 124

After

flutter analyze patient/   # 0 use_build_context_synchronously warnings
flutter analyze therapist/ # 0 use_build_context_synchronously warnings

Checklist

  • flutter analyze run after all changes — no new warnings introduced
  • if (!mounted) return placed immediately after each await before any context access
  • Pre-capture pattern used where mounted check alone is insufficient (build callback context)

Summary by CodeRabbit

  • Bug Fixes

    • Fixed potential crashes by preventing UI operations (SnackBars, navigation) from executing after widgets are disposed.
    • Added safety checks for async operations to ensure context validity before updating the interface.
  • Refactor

    • Improved code safety in appointment booking and authentication flows by capturing UI dependencies before async gaps.

Resolves use_build_context_synchronously warnings in patient and
therapist apps:
- patient/book_appointment.dart: move mounted check before context use;
  refactor _selectTimeSlot to use State.context directly
- patient/therapist_provider.dart: add context.mounted guard before
  ScaffoldMessenger calls after await
- therapist/personal_details_screen.dart: pre-capture Navigator and
  ScaffoldMessenger before await; use single if (!mounted) return guard

Closes AOSSIE-Org#197
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 13, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f35b2d9e-2471-449a-b677-3af5f921b31f

📥 Commits

Reviewing files that changed from the base of the PR and between 051a4f3 and 957f0c7.

📒 Files selected for processing (3)
  • patient/lib/presentation/result/book_appointment.dart
  • patient/lib/provider/therapist_provider.dart
  • therapist/lib/presentation/auth/personal_details_screen.dart

📝 Walkthrough

Walkthrough

Fixes four instances of use_build_context_synchronously linting warnings across the patient and therapist apps by refactoring BuildContext usage across async gaps. Changes include removing context parameters, capturing context-dependent objects pre-async, and adding mounted guards before context operations.

Changes

Cohort / File(s) Summary
Patient App - Book Appointment Logic
patient/lib/presentation/result/book_appointment.dart, patient/lib/provider/therapist_provider.dart
Removes BuildContext parameter from _selectTimeSlot() method and adds if (context.mounted) guard before displaying SnackBar in error/success paths within bookAppointment. Prevents context usage after async operations on potentially unmounted widgets.
Therapist App - Auth Context Capture
therapist/lib/presentation/auth/personal_details_screen.dart
Captures Navigator and ScaffoldMessenger references before async gap, replaces direct context-based navigation and SnackBar calls with captured references, and adds post-await if (!mounted) return guard to prevent context access on unmounted widgets.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 Context must not leap the async abyss,
Mount a guard where the await calls amiss!
Captured before the gap, swift and true,
A rabbit's fix for a timely brew. ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main fix: guarding BuildContext usage across async gaps to resolve use_build_context_synchronously warnings.
Linked Issues check ✅ Passed All requirements from issue #197 are addressed: mounted guards added after awaits in book_appointment.dart and therapist_provider.dart, context objects pre-captured in personal_details_screen.dart, and flutter analyze confirms zero warnings.
Out of Scope Changes check ✅ Passed All changes directly address the use_build_context_synchronously issue; no unrelated modifications detected beyond the scope of fixing async gap context usage.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ 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.

fix: Don't use BuildContext across async gaps (use_build_context_synchronously)

1 participant