@@ -65,6 +65,7 @@ class AuthState extends AppState {
6565 if (_profileQuery == null ) {
6666 _profileQuery = kDatabase.child ("profile" ).child (user! .uid);
6767 _profileQuery! .onValue.listen (_onProfileChanged);
68+ _profileQuery! .onChildChanged.listen (_onProfileupdated);
6869 }
6970 } catch (error) {
7071 cprint (error, errorIn: 'databaseInit' );
@@ -423,12 +424,35 @@ class AuthState extends AppState {
423424 /// Trigger when logged-in user's profile change or updated
424425 /// Firebase event callback for profile update
425426 void _onProfileChanged (DatabaseEvent event) {
426- // if (event.snapshot != null) {
427427 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+ }
433457 }
434458}
0 commit comments