Flutter Bloc I Can't Logout

Viewed 38

I'm just started with flutter bloc. I want to make a movie listing app, create your own lists and share your friends etc. The problem is, when i tapped to logout button, UI does not update.

Here's where i try to change the UI. If the state is Authenticated(), I'm returning WatchlistNavBar(), if the state is Unauthenticated() I'm returning WelcomeView() for login or register. If the user has submitted email and password correctly, WatchlistNavBar() is building. Everything works fine. But when the user tries the logout, WelcomeView() does not build. By the way BlocNavigate() class is called in MaterialApp()'s home property.

class BlocNavigate extends StatelessWidget {
  const BlocNavigate({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<AuthBloc, AuthState>(
      builder: (context, state) {
        if (state is Loading) {
          return const LoadingWidget();
        } else if (state is Authenticated) {
          return const WatchlistNavBar();
        } else if (state is Unauthenticated) {
          return const WelcomeView();
        } else {
          return const SignInView();
        }
      },
    );
  }
}

AuthBloc:

class AuthBloc extends Bloc<AuthEvent, AuthState> {
  AuthRepository authRepository = AuthRepository();

  AuthBloc(this.authRepository) : super(AuthInitial()) {
    on<AuthenticationStarted>(_onAuthStarted);
    on<AuthenticationSignedOut>(_onSignOut);
  }

  _onAuthStarted(AuthenticationStarted event, Emitter<AuthState> emit) async {
    UserModel user = await authRepository.getCurrentUser().first;
    if (user.uid != "uid") {
      emit(Authenticated());
    } else {
      emit(Unauthenticated());
    }
  }

  _onSignOut(AuthenticationSignedOut event, Emitter<AuthState> emit) async {
    authRepository.signOut();
    emit(Unauthenticated());
  }
}

AuthState:

abstract class AuthState extends Equatable {
  const AuthState();
  @override
  List<Object> get props => [];
}

class AuthInitial extends AuthState {}

class Authenticated extends AuthState {}

class Unauthenticated extends AuthState {}

class Loading extends AuthState {}

And this is the logout button, where i add AuthenticationSignedOut() to AuthBloc():

class LogoutButton extends StatelessWidget {
  const LogoutButton({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return IconButton(
        icon: const Icon(Icons.exit_to_app, color: Colors.black),
        onPressed: () {
          context.read<AuthBloc>().add(AuthenticationSignedOut());
        });
  }
}

My main function and MaterialApp():

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);

  Bloc.observer = AppBlocObserver();
  runApp(
    MultiBlocProvider(providers: [
      BlocProvider(create: (context) => FormBloc()),
      BlocProvider(create: (context) => DatabaseBloc(DatabaseRepositoryImpl())),
      BlocProvider(
          create: (context) =>
              AuthBloc(AuthRepository())..add(const AuthenticationStarted())),
      BlocProvider(
          create: (context) => FavoritesBloc()..add(const FavoritesLoad()))
    ], child: const WatchlistApp()),
  );
}

class WatchlistApp extends StatelessWidget {
  const WatchlistApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Watchlist',
      theme: WatchlistTheme.mainTheme,
      home: const BlocNavigate(),
    );
  }
}

As i said, i'm new to flutter bloc and don't know exactly what I'm doing wrong. If you need more information please let me know.

0 Answers
Related