正确使用腕尺
Proper use of cubit
我仍在学习如何使用 cubit 和 bloc,我正在尝试在我的项目中使用 cubit,但我对如何使用它有点困惑。
有一个屏幕需要 phone 号码,我使用库“intl_phone_number_input”来格式化、验证和 select 国家/地区。当我单击下一页按钮时,它需要检查 phone 是否有效,但我需要一个存储此信息的变量。 widged InternationalPhoneNumberInput 有一个 属性 onInputValidated 如果 phone 号码有效,则 returns 为真,那么我应该在哪里创建这个变量?我应该在我的小部件 class 中还是在手肘内创建它?我在 cubit 内部创建,但我不确定它是否是正确的方法,所以我得到了这个:
onInputValidated: (bool value) {
BlocProvider.of<LoginCubit>(context).isValid =
value;
},
我已经研究并看到了一些关于 cubit 以及如何使用它们的示例,但我仍然完全不明白,因为在这些示例中, cubit 从未使用过变量,所有变量都变成了一个状态,但是在我的例子中,我需要这个值作为变量。
我也对如何使用 cubit 显示对话框感到困惑,我已经这样做了:
@override
Widget build(BuildContext context) {
return BlocConsumer<LoginCubit, LoginState>(
listenWhen: (previous, current) => current is ShowDialogErrorLoginState || current is NavigateFromLoginStateToHomePageState,
listener: (context, state) {
if (state is ShowDialogErrorLoginState) {
showErrorDialog(context, state.titleMessage, state.bodyMessage);
}
if (state is NavigateFromLoginStateToHomePageState) {
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (context) => const MyHomePage()));
}
},
builder: (context, state) {
if (state is ShowLoginState) {
return buildPhoneForm(context);
}
if (state is SendingCodeLoginState) {
return ProgressView(message: 'Sending SMS code',);
}
if (state is ShowCodeLoginState) {
return buildCodeForm(context);
}
return const ErrorView('Unknown error');
},
);
}
在我的肘部,我做了以下事情:
void goToCodeVerification(String phoneNumber) async {
if (!isValid){
String titleMessage = "Phone number invalid";
String bodyMessage = "The given phone number is invalid";
emit(ShowDialogErrorLoginState(titleMessage, bodyMessage));
emit(ShowLoginState());
} else {
emit(SendingCodeLoginState());
// TO DO
// use API to send a code
emit(ShowCodeLoginState());
}
}
这是用 cubit 显示对话框的正确方法吗?
好的,所以你有一个要使用的值,变量不影响状态,你需要在你的手肘内访问它。
对于这样的事情,我认为将变量存储在肘上最有意义,但请记住,对于这种简单的情况,任何一种方法都是可以接受的。
我也不太喜欢下面代码的样子:
onInputValidated: (bool value) {
BlocProvider.of<LoginCubit>(context).isValid =
value;
},
有点笨拙,我更愿意将整个回调移动到肘部:
void onInputValidated(bool value) => isValid = value;
这样:
final cubit = BlocProvider.of<LoginCubit>(context);
...
onInputValidated: cubit.onInputValidated,
我仍在学习如何使用 cubit 和 bloc,我正在尝试在我的项目中使用 cubit,但我对如何使用它有点困惑。
有一个屏幕需要 phone 号码,我使用库“intl_phone_number_input”来格式化、验证和 select 国家/地区。当我单击下一页按钮时,它需要检查 phone 是否有效,但我需要一个存储此信息的变量。 widged InternationalPhoneNumberInput 有一个 属性 onInputValidated 如果 phone 号码有效,则 returns 为真,那么我应该在哪里创建这个变量?我应该在我的小部件 class 中还是在手肘内创建它?我在 cubit 内部创建,但我不确定它是否是正确的方法,所以我得到了这个:
onInputValidated: (bool value) {
BlocProvider.of<LoginCubit>(context).isValid =
value;
},
我已经研究并看到了一些关于 cubit 以及如何使用它们的示例,但我仍然完全不明白,因为在这些示例中, cubit 从未使用过变量,所有变量都变成了一个状态,但是在我的例子中,我需要这个值作为变量。
我也对如何使用 cubit 显示对话框感到困惑,我已经这样做了:
@override
Widget build(BuildContext context) {
return BlocConsumer<LoginCubit, LoginState>(
listenWhen: (previous, current) => current is ShowDialogErrorLoginState || current is NavigateFromLoginStateToHomePageState,
listener: (context, state) {
if (state is ShowDialogErrorLoginState) {
showErrorDialog(context, state.titleMessage, state.bodyMessage);
}
if (state is NavigateFromLoginStateToHomePageState) {
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (context) => const MyHomePage()));
}
},
builder: (context, state) {
if (state is ShowLoginState) {
return buildPhoneForm(context);
}
if (state is SendingCodeLoginState) {
return ProgressView(message: 'Sending SMS code',);
}
if (state is ShowCodeLoginState) {
return buildCodeForm(context);
}
return const ErrorView('Unknown error');
},
);
}
在我的肘部,我做了以下事情:
void goToCodeVerification(String phoneNumber) async {
if (!isValid){
String titleMessage = "Phone number invalid";
String bodyMessage = "The given phone number is invalid";
emit(ShowDialogErrorLoginState(titleMessage, bodyMessage));
emit(ShowLoginState());
} else {
emit(SendingCodeLoginState());
// TO DO
// use API to send a code
emit(ShowCodeLoginState());
}
}
这是用 cubit 显示对话框的正确方法吗?
好的,所以你有一个要使用的值,变量不影响状态,你需要在你的手肘内访问它。
对于这样的事情,我认为将变量存储在肘上最有意义,但请记住,对于这种简单的情况,任何一种方法都是可以接受的。
我也不太喜欢下面代码的样子:
onInputValidated: (bool value) {
BlocProvider.of<LoginCubit>(context).isValid =
value;
},
有点笨拙,我更愿意将整个回调移动到肘部:
void onInputValidated(bool value) => isValid = value;
这样:
final cubit = BlocProvider.of<LoginCubit>(context);
...
onInputValidated: cubit.onInputValidated,