fix: resolve BuildContext async gaps across patient and therapist apps#210
fix: resolve BuildContext async gaps across patient and therapist apps#210suhas2006-code wants to merge 1 commit intoAOSSIE-Org:mainfrom
Conversation
Fixes AOSSIE-Org#197 ## Changes **Patient App:** - Fixed BuildContext usage in `book_appointment.dart` by refactoring `_selectDate()` to use instance context and added proper mounted checks - Fixed BuildContext usage in `therapist_provider.dart` by capturing ScaffoldMessengerState before async gaps **Therapist App:** - Fixed BuildContext usage in `personal_details_screen.dart` by capturing Navigator and ScaffoldMessenger states before async operations ## Technical Details - Resolved "Don't use BuildContext across async gaps" warnings by capturing widget context before await calls - Added proper mounted checks to prevent memory leaks and context usage after widget disposal - Ensures UI operations (navigation, snackbars) are safely performed after async operations ## Testing Verified with `flutter analyze` - no use_build_context_synchronously warnings remain in either app. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
📝 WalkthroughWalkthroughThree files updated to fix Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment Tip You can customize the tone of the review comments and chat replies.Configure the |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
patient/lib/provider/therapist_provider.dart (1)
1-7:⚠️ Potential issue | 🟡 MinorDuplicate import of
flutter/material.dart.Line 1 and Line 6 both import the same package. Remove one of the duplicates.
🧹 Proposed fix
import 'package:flutter/material.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:patient/presentation/assessments/models/assessment_card_model.dart'; import 'package:patient/repository/supabase_assessments_repository.dart'; -import 'package:flutter/material.dart'; import 'package:supabase_flutter/supabase_flutter.dart';🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@patient/lib/provider/therapist_provider.dart` around lines 1 - 7, Remove the duplicate import of flutter/material.dart in therapist_provider.dart by deleting one of the two identical lines (either the first or the sixth import) so only a single import 'package:flutter/material.dart' remains; after removal, ensure the remaining imports (including dotenv, assessment_card_model, supabase_assessments_repository, and supabase_flutter) are correctly ordered and there are no unused import statements left.
🧹 Nitpick comments (2)
patient/lib/presentation/result/book_appointment.dart (2)
49-59: Redundantmountedcheck on line 57.The
mountedcheck on line 50 already guards against widget disposal - if not mounted, the function returns early. The subsequent check on line 57 is therefore redundant sincesetStateon lines 52-55 would have already been skipped or the function would have returned.🧹 Proposed simplification
if (picked == null || picked == selectedDate) return; if (!mounted) return; setState(() { selectedDate = picked; selectedTimeSlot = null; // Reset time slot when date changes }); - if (mounted) { - _selectTimeSlot(); - } + _selectTimeSlot(); }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@patient/lib/presentation/result/book_appointment.dart` around lines 49 - 59, Remove the redundant mounted check after setState: the early return if (!mounted) already prevents running on a disposed widget, so delete the if (mounted) { _selectTimeSlot(); } block and call _selectTimeSlot() directly after setState (keeping the setState that updates selectedDate and resets selectedTimeSlot) so the flow is: validate picked, return if not mounted, setState(...), then _selectTimeSlot().
63-85: Consider usingctxinNavigator.popfor consistency.Inside the
buildercallback, you have access toctx(the builder's context). UsingNavigator.pop(ctx)instead ofNavigator.pop(context)on line 77 would be more consistent, though both work correctly in this case.🧹 Proposed fix
onTap: () { setState(() { selectedTimeSlot = slot; }); - Navigator.pop(context); // Close modal + Navigator.pop(ctx); // Close modal },🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@patient/lib/presentation/result/book_appointment.dart` around lines 63 - 85, The modal builder uses a local BuildContext variable named ctx but the code calls Navigator.pop(context); update _selectTimeSlot to call Navigator.pop(ctx) inside the builder's onTap so the pop uses the builder's context (replace Navigator.pop(context) with Navigator.pop(ctx) in the ListTile onTap within the builder closure).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@patient/lib/provider/therapist_provider.dart`:
- Around line 1-7: Remove the duplicate import of flutter/material.dart in
therapist_provider.dart by deleting one of the two identical lines (either the
first or the sixth import) so only a single import
'package:flutter/material.dart' remains; after removal, ensure the remaining
imports (including dotenv, assessment_card_model,
supabase_assessments_repository, and supabase_flutter) are correctly ordered and
there are no unused import statements left.
---
Nitpick comments:
In `@patient/lib/presentation/result/book_appointment.dart`:
- Around line 49-59: Remove the redundant mounted check after setState: the
early return if (!mounted) already prevents running on a disposed widget, so
delete the if (mounted) { _selectTimeSlot(); } block and call _selectTimeSlot()
directly after setState (keeping the setState that updates selectedDate and
resets selectedTimeSlot) so the flow is: validate picked, return if not mounted,
setState(...), then _selectTimeSlot().
- Around line 63-85: The modal builder uses a local BuildContext variable named
ctx but the code calls Navigator.pop(context); update _selectTimeSlot to call
Navigator.pop(ctx) inside the builder's onTap so the pop uses the builder's
context (replace Navigator.pop(context) with Navigator.pop(ctx) in the ListTile
onTap within the builder closure).
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: fdad5149-53e0-478a-ace6-39b3773cc9a2
📒 Files selected for processing (3)
patient/lib/presentation/result/book_appointment.dartpatient/lib/provider/therapist_provider.darttherapist/lib/presentation/auth/personal_details_screen.dart
Summary
Resolves #197 by fixing all BuildContext usage across async gaps in both patient and therapist applications.
Changes Made
Patient App
book_appointment.dart:_selectDate()method to use instance context instead of parametermountedcheck before using BuildContext after async operationstherapist_provider.dart:ScaffoldMessengerStatebefore any async gaps to prevent context usage issuesawaitcallTherapist App
personal_details_screen.dart:NavigatorandScaffoldMessengerstates before async operationsmountedcheck after async callTechnical Details
awaitcalls and add mounted checksTesting
flutter analyzeon both apps - nouse_build_context_synchronouslywarnings remainQuality Assurance
🤖 Generated with Claude Code
Summary by CodeRabbit
Release Notes