繁体   English   中英

我在使用 Bloc/cubit 在 flutter 的其他小部件/类中使用 BlocBuilder 获取上次发出的 state 时遇到问题

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

我有一个cubit的登录,它将接受两个参数,email和密码。 然后它会要求存储库给他该请求的响应。 在调用存储库的 function 之前,我发出加载 state,当我从存储库收到数据时,发出另一个 state,即 SigninLoaded。 我的登录Cubit:

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;
          }

But when I try to access response or the last emitted state which was SigninLoaded, I get initial state in other class, I tried to get response in Profile class like this;

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,
                    ),
                  );
                },
              ),

我没有得到回复,因为它说它在初始 state 中。

即使我也像这样提供了 Blocs;

BlocProvider 到 LoginPage class;

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

BlocProvider 用于配置 class;

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

可能是我错了,但在我看来,如果你想在 Profile 页面中使用 SignInModel,你必须再次调用 signIn function

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

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

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

我有一个类似的问题。 我有一个适合我的临时解决方法,我将emit包装在 Future.delayed 中,就像这样

Future.delayed(Duration.zero,()=>emit(<<newState>>));

然后 My BlocBuilder 识别 state 更改。 我不知道为什么,问题是,我的代码曾经运行良好,然后因看似无关的更改而停止。 希望这会有所帮助,但更重要的是,我希望真正知道发生了什么的人可以告诉我我应该做什么来修复我的代码:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM