我是 Flutter 的新手,使用 Block 进行状态管理。我有两个屏幕
- 注册画面
- 验证屏幕
在“RegistrationScreen”中,我添加了我的 BlockConsumer,如下所示
child: Center(
child: Column(
children: [
BlocConsumer<RegistrationCubit, RegistrationState>(
builder: (context, state) {
return state.maybeWhen(null,
setUpRegistration: () =>
createRegistrationForm(context),
orElse: () {
return createRegistrationForm(context);
});
},
listener: (context, state) {
state.maybeWhen(null,
registrationInProgress: () => {
showLoader(true),
},
registrationResponseReceived: (response) =>
requestForOTPOnSuccess(context, response),
registrationFailedGeneric: () =>
registrationFailureGeneric(
context,
"Error in registration",
"Something went wrong"),
orElse: () {});
},
),
],
),
),
一旦我们从服务中获得成功,我们就会从“RegistrationScreen”推送到“VerifyScreen”
在“VerifyScreen”中,我试图在 BlockListner 中重用 RegistrationCubit,如下所示(单击按钮再次调用注册服务)
BlocListener<RegistrationCubit, RegistrationState>(
listener: (context, st) {
st.maybeWhen(null,
registrationInProgress: () => {
showLoader(true),
},
registrationResponseReceived: (_) => showGenericAlert(
context,
'My App',
'Code sent mobile number.'),
registrationFailedGeneric: () => showGenericAlert(context,
'Error', 'An error occurred. Please try again later.'),
orElse: () {});
},
child: Container(),
)
但不是调用“VerifyScreen”BlocListener,而是调用“RegistrationScreen”侦听器。
您能否指导为什么从“VerifyScreen”调用“RegistrationScreen”侦听器?