我在使用 Bloc/cubit 的其他 widgets/classes 中使用 BlocBuilder 获取最后发出的状态时遇到问题

I am having trouble getting last emitted state with BlocBuilder in other widgets/classes in flutter using Bloc/cubit

我有一个登录cubit,需要两个参数,邮箱和密码。然后它会要求存储库给他对该请求的响应。在调用存储库函数之前,我发出加载状态,当我从存储库接收到数据时,发出另一个状态,即 SigninLoaded。我的 SigninCubit:

final SignInRepository signInRepository;

SigninCubit({required this.signInRepository}) : super(SigninInitial());

Future<void> signIn({String email = '', String password = ''}) async {
  print("Cubit is invoked");
  emit(SigninInLoading());
  Timer(Duration(seconds: 0), () async {
    await signInRepository
        .signIn(email: email, password: password)
        .then((signinModel) {
      // print("SignIn Model is : ${signinModel}");‹‹
      emit(SigninLoaded(signInModel: signinModel));
    });
  });
}
}

登录状态

@immutable
abstract class SigninState {}

class SigninInitial extends SigninState {}

class SigninInLoading extends SigninState {}

class SigninLoaded extends SigninState {
  SignInModel? signInModel;
  SigninLoaded({required this.signInModel});
}

这就是我从 LoginPage class 调用我的 signinCubit 的方式。

BlocProvider.of<SigninCubit>(context).signIn(email: email, password: password);

当从 LoginPage class 调用 signinCubit 时,我看到它在同一个 class 中使用 BlocBuilder 进行响应;

BlocBuilder<SigninCubit, SigninState>(
        builder: (context, state) {
          if ((state is SigninInitial)) {
            print("Initial State");
          } else if (state is SigninInLoading) {
            return Center(
              child: CircularProgressIndicator(
                color: AppColors.mainColor,
              ),
            );
          } else {
            final signinData = (state as SigninLoaded).signInModel;

            data = signinData;
          }

但是当我尝试访问响应或最后发出的状态 SigninLoaded 时,我在其他 class 中获得初始状态,我试图在配置文件 class 中获得响应,就像这样;

BlocBuilder<SigninCubit, SigninState>(
                builder: (context, state) {
                  print("State : $state");
                  if (state is SigninLoaded) {
                    name = (state as SigninLoaded).signInModel?.data?.firstName;
                    print("Name $name");
                  }
                  return Text(
                    'Welcome, ${name}!',
                    style: TextStyle(
                      color: Colors.black,
                      fontSize: 20,
                      fontWeight: FontWeight.w800,
                    ),
                  );
                },
              ),

我没有收到回复,因为它说它处于初始状态。

尽管我也提供了Blocs;

登录页面的 BlocProvider class;

MaterialPageRoute(
          builder: (_) => BlocProvider(
            create: (BuildContext context) =>
                SigninCubit(signInRepository: signInRepository),
            child: LoginPage(),
          ),
        );

配置文件的 BlocProvider class;

 MaterialPageRoute(
            builder: (_) => BlocProvider(
                  create: (context) =>
                      SigninCubit(signInRepository: signInRepository),
                  child: Profile(),
                ));

可能我错了,但在我看来,如果你想在个人资料页面中使用 SignInModel,你必须再次调用 signIn 函数

  • 另一种解决方案:只定义一个 SignInCubit 的 BlocProvider 作为 Profile 页面和 SignIn 页面的父级,例如:

    材质页面路由( 建设者:(_)=> BlocProvider( 创建:(上下文)=> SigninCubit(signInRepository:signInRepository), child: 我的应用程序(), ));

  • 将signinModel或(登录信息)保存在本地数据库中,我们进入ProfilePage时,从db中获取