Skip to content

Latest commit

 

History

History
113 lines (96 loc) · 2.95 KB

File metadata and controls

113 lines (96 loc) · 2.95 KB

Minimal Code Changes for Button State Fix

File: lib/Screens/parent_dashboard.dart

Change 1: Update initState() (lines ~45-55)

OLD:

@override
void initState() {
  super.initState();
  _tabController = TabController(length: 2, vsync: this);
  _loadParentData();
  
  Future.delayed(Duration.zero, () async {
    final uid = await AuthService.getCurrentUserId();
    if (uid != null && mounted) {
      context.read<LinkingStateNotifier>().initialize(uid);
    }
  });
}

NEW:

@override
void initState() {
  super.initState();
  _tabController = TabController(length: 2, vsync: this);
  _loadParentData();
  
  // Initialize LinkingStateNotifier with parent ID
  Future.delayed(Duration.zero, () async {
    final uid = await AuthService.getCurrentUserId();
    if (uid != null && mounted) {
      final notifier = context.read<LinkingStateNotifier>();
      notifier.initialize(uid);
      // FIX: Listen to linking state changes and update local list
      notifier.addListener(_syncLinkedChildrenFromNotifier);
    }
  });
}

Change 2: Update dispose() (lines ~538-545)

OLD:

void dispose() {
  _tabController.dispose();
  super.dispose();
}

NEW:

@override
void dispose() {
  _tabController.dispose();
  // FIX: Remove listener when screen closes
  try {
    context.read<LinkingStateNotifier>().removeListener(_syncLinkedChildrenFromNotifier);
  } catch (_) {}
  super.dispose();
}

Change 3: Add new method (after dispose(), before build())

/// FIX: Sync local _linkedChildren with LinkingStateNotifier when it changes
/// This ensures buttons and dialogs show correct children immediately after linking
void _syncLinkedChildrenFromNotifier() {
  final notifier = context.read<LinkingStateNotifier>();
  setState(() {
    _linkedChildren = notifier.linkedChildren
        .map((linkedChild) => {
              'username': linkedChild.childUsername,
              'personalId': linkedChild.childEmail,
              'uid': linkedChild.childId,
              'linkedAt': linkedChild.linkedAt,
            })
        .toList();
  });
  print('✅ Synced local _linkedChildren from notifier: ${_linkedChildren.length} children');
}

No Other Files Modified

  • LinkingService ✅ (unchanged)
  • LinkingStateNotifier ✅ (unchanged)
  • ChildLinkingScreen ✅ (unchanged)
  • TaskSyncService ✅ (unchanged)
  • Firestore rules ✅ (unchanged)
  • UI buttons & dialogs ✅ (unchanged - they now just receive correct data)

How to Apply

  1. Open lib/Screens/parent_dashboard.dart
  2. Locate the existing initState() method (around line 45-55)
  3. Add notifier.addListener(_syncLinkedChildrenFromNotifier); in the initialization
  4. Locate the existing dispose() method (around line 538-545)
  5. Add the listener removal and @override decorator
  6. Insert the new _syncLinkedChildrenFromNotifier() method right after dispose()
  7. Save and rebuild: flutter build windows

Done. No other changes needed.