@@ -65,6 +65,7 @@ class AuthState extends AppState {
65
65
if (_profileQuery == null ) {
66
66
_profileQuery = kDatabase.child ("profile" ).child (user! .uid);
67
67
_profileQuery! .onValue.listen (_onProfileChanged);
68
+ _profileQuery! .onChildChanged.listen (_onProfileupdated);
68
69
}
69
70
} catch (error) {
70
71
cprint (error, errorIn: 'databaseInit' );
@@ -423,12 +424,35 @@ class AuthState extends AppState {
423
424
/// Trigger when logged-in user's profile change or updated
424
425
/// Firebase event callback for profile update
425
426
void _onProfileChanged (DatabaseEvent event) {
426
- // if (event.snapshot != null) {
427
427
final val = event.snapshot.value;
428
- final updatedUser = UserModel .fromJson (val as Map );
429
- _userModel = updatedUser;
430
- cprint ('UserModel Updated' );
431
- notifyListeners ();
432
- // }
428
+ if (val is Map ) {
429
+ final updatedUser = UserModel .fromJson (val);
430
+ _userModel = updatedUser;
431
+ cprint ('UserModel Updated' );
432
+ getIt <SharedPreferenceHelper >().saveUserProfile (_userModel! );
433
+ notifyListeners ();
434
+ }
435
+ }
436
+
437
+ void _onProfileupdated (DatabaseEvent event) {
438
+ final val = event.snapshot.value;
439
+ if (val is List &&
440
+ ['following' , 'followers' ].contains (event.snapshot.key)) {
441
+ final list = val.cast <String >().map ((e) => e).toList ();
442
+ if (event.previousChildKey == 'following' ) {
443
+ _userModel = _userModel! .copyWith (
444
+ followingList: val.cast <String >().map ((e) => e).toList (),
445
+ following: list.length,
446
+ );
447
+ } else if (event.previousChildKey == 'followers' ) {
448
+ _userModel = _userModel! .copyWith (
449
+ followersList: list,
450
+ followers: list.length,
451
+ );
452
+ }
453
+ getIt <SharedPreferenceHelper >().saveUserProfile (_userModel! );
454
+ cprint ('UserModel Updated' );
455
+ notifyListeners ();
456
+ }
433
457
}
434
458
}
0 commit comments