Home > Back-end >  flutter_bloc: ^8.0.0 state does not updating
flutter_bloc: ^8.0.0 state does not updating

Time:12-08

I have a bloc that keeps user data -via firebase- on the state. It works as it is but when the user signs out and sign in again, I can't update the bloc with new user data.

Eg.

User_1 (name: one) signs out. Then User_2 (name: two) signs in. Bloc state still keeping data from User_1

user_state.dart

part of 'user_bloc.dart';

class UserState extends Equatable {
  const UserState();

  @override
  List<Object?> get props => [];
}

class UserLoading extends UserState {
  const UserLoading();

  @override
  List<Object> get props => [];
}

class UserLoaded extends UserState {
  final UserFindine user;

  UserLoaded({required this.user});

  @override
  List<Object> get props => [user];
}

user_event.dart

part of 'user_bloc.dart';

class UserEvent extends Equatable {
  const UserEvent();

  @override
  List<Object> get props => [];
}

class LoadUser extends UserEvent {
  const LoadUser();

  @override
  List<Object> get props => [];
}

class UpdateUser extends UserEvent {
  final UserFindine user;

  const UpdateUser(this.user);

  @override
  List<Object> get props => [user];
}

user_bloc.dart

class UserBloc extends Bloc<UserEvent, UserState> {
  final DatabaseRepository _databaseRepository;
  StreamSubscription? _databaseSubscription;

  UserBloc({
    required DatabaseRepository databaseRepository,
  })  : _databaseRepository = databaseRepository,
        super(UserLoading()) {
    on<LoadUser>((event, emit) {
      _databaseSubscription?.cancel();
      _databaseSubscription = _databaseRepository.getUser().listen((user) {
        add(UpdateUser(user));
      });
    });
    on<UpdateUser>((event, emit) {
      final data = event.user; // This data is right.
      emit(UserLoaded(user: data));
    });
  }
}

I've tried to trigger LoadUser event when the user signs in. But still getting old user data. Am I missing something?

Thanks for any help

  • Related