Search code examples
flutterblocfreezed

Cubit state shows 2 different values


I have basic auth cubit like this:

class AuthCubit extends Cubit<AuthState> {
  AuthCubit() : super(const _AuthState(isUserSignedIn: false)) {
    FirebaseAuth.instance.authStateChanges().listen((User? user) {
      if (user == null) {
        emit(state.copyWith(
          isUserSignedIn: false,
        ));

        print('user logged out');
      } else {
        print('user logged innnnn');
        emit(state.copyWith(isUserSignedIn: true));
      }
      print(state);
    });
  }
}

When I print the statement above, it works correctly (When I log in, it gives this: AuthState(isUserSignedIn: true)

Now, the problem starts here: Main.dart

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: BlocProvider(
      lazy: false,
      create: (context) => AuthCubit(),
      child: FlowBuilder<AuthState>(
          state: const AuthState(),
          onGeneratePages: (authState, pages) {
            print(("HERE MAIN.DART AuthState ${authState}"));
            if (authState.isUserSignedIn) {
              return [SignInPage.page()];
            } else {
              return [SignInPage.page()];
            }
          }),
    ));
  }
}

I use flow builder. HERE MAIN.DART section of the code, gives false, although the real statement true. By the way, auth state has default value (false), because of this it can give maybe I do not know.

Auth State here:

@freezed
class AuthState with _$AuthState {
  const factory AuthState({
    @Default(false) bool isUserSignedIn,
  }) = _AuthState;
}

Solution

  • I solved with the flow_builder package.