我正在尝试使用 dartz 获取电子邮件地址验证器的值,但输出始终在左侧,我尝试了相同的代码,使用相同的正则表达式将验证放入验证器中,效果很好!我正在使用 bloc 模式、冻结和 dartz 库。
电子邮件地址代码:
class _EmailAdressInput extends _ListOfInputs {
const _EmailAdressInput();
@override
Widget build(BuildContext context) {
return BlocBuilder<AuthBloc, AuthBlocState>(builder: (context, state) {
return TextFormField(
focusNode: emailAddressNode,
autovalidateMode: AutovalidateMode.onUserInteraction,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelText: 'Email Address',
hintText: 'For Example: user@gmail.com',
contentPadding: EdgeInsets.all(
MediaQuery.of(context).size.width * 0.01,
),
),
validator: (_) {
return state
.emailAdressState
.value
.fold((l) => l.msg, (_) => null);
// if (!RegExp(r"""^(\w|\.|\_|\-)+[@](\w|\_|\-|\.)+[.]\w{2,3}$""")
// .hasMatch(input!)) {
// return 'false';
// }
},
onChanged: (value) {
return context.read<AuthBloc>().add(
AuthBlocEvent.emailAdressPressed(emailAdressPressedValue: value));
},
);
});
}
}
EmailAdress 对象的代码:
@immutable
class EmailAdress extends ValueObject {
@override
final Either<AuthFalires,String> value ;
factory EmailAdress({required String emailInput}){
return EmailAdress._( emailValidation( input: emailInput));
}
const EmailAdress._(this.value);
}
验证器的代码:
Either<AuthFalires,String> emailValidation({required String input}){
if (RegExp(r"""^(\w|\.|\_|\-)+[@](\w|\_|\-|\.)+[.]\w{2,3}$""").hasMatch(input)) {
return right(input);
}else{
return left(const AuthFalires.invalideEmail(msg:'Invalid Email'));
}
}
区块代码:
class AuthBloc extends Bloc<AuthBlocEvent, AuthBlocState> {
AuthBloc() : super(AuthBlocState.initial());
@override
Stream<AuthBlocState> mapEventToState(
AuthBlocEvent event,
) async* {
yield* event.map(
emailAdressPressed: (e) async* {
state.copyWith(
emailAdressState: EmailAdress(
emailInput: e.emailAdressPressedValue,
),
);
});
}
}
事件代码:
@freezed
class AuthBlocEvent with _$AuthBlocEvent {
const factory AuthBlocEvent.emailAdressPressed({required String emailAdressPressedValue }) =
_EmailAdressPressed;
}
州代码是:
@freezed
class AuthBlocState with _$AuthBlocState {
const factory AuthBlocState(
{required EmailAdress emailAdressState}) = _AuthBlocState;
factory AuthBlocState.initial() => AuthBlocState(
emailAdressState: EmailAdress(emailInput: ''),
);
}
AuthFailure 代码是:
@freezed
abstract class AuthFalires with _$AuthFalires {
const factory AuthFalires.invalideEmail({required String msg}) =
_InvalideEmail;
}