文本验证器无法使用bloc并冻结
使用 DDD 模式无法验证电子邮件地址,我正在使用 dartz、bloc 和冻结来这样做。我试图发现错误,但我不能。
电子邮件地址是:
class _EmailAdressInput extends _ListOfInputs {
const _EmailAdressInput();
@override
Widget build(BuildContext context) {
return BlocBuilder<AuthBloc, AuthBlocState>(builder: (context, state) {
return TextFormField(
focusNode: emailAddressNode,
autocorrect: false,
textInputAction: TextInputAction.next,
decoration: InputDecoration(
labelText: 'Email Address',
),
validator: (value) {
return context.read<AuthBloc>().state.email.value!.fold(
(l) => l.maybeMap(
orElse: () {}, invalidEmail: (_) => "Invalid Email"),
(r) => null);
},
onChanged: (value) {
return context.read<AuthBloc>().add(
AuthBlocEvent.emailAddressEvent(emailAdressEventValue: value));
},
onFieldSubmitted: (value) {
emailAddressNode!.unfocus();
FocusScope.of(context).requestFocus(passwordNode);
},
);
});
}
}
使用冻结的事件:
@freezed
abstract class AuthBlocEvent with _$AuthBlocEvent {
const factory AuthBlocEvent.emailAddressEvent(
{required String emailAdressEventValue}) = _EmailAddressEvent;
}
使用冻结库的状态是:
@freezed
abstract class AuthBlocState with _$AuthBlocState {
const factory AuthBlocState({
required Password password,
required bool isSubmitted,
required bool showErrorMessages,
required Option<Either<AuthFailures, Unit>> authFailureOrSuccessOption,
}) = _AuthBlocState;
factory AuthBlocState.initial() => AuthBlocState(
email: EmailAddress(''),
isSubmitted: false,
showErrorMessages: false,
authFailureOrSuccessOption: none(),
);
}
该集团是:
@override
Stream<AuthBlocState> mapEventToState(
AuthBlocEvent event,
) async* {
yield* event.map(
emailAddressEvent: (e) async* {
yield state.copyWith(
email: EmailAddress(e.emailAdressEventValue),
showErrorMessages: true,
authFailureOrSuccessOption: none(),
);
},
okPressedEvent: (e) async* {
// to get the value of
// either we should use option of
// to check either values
// we have to knew that
// both are right
if (state.email.isValueValid()
) {
yield state.copyWith(
isSubmitted: true,
authFailureOrSuccessOption: none(),
);
}
},
);}
验证器是:
Either<ValueFailures, String> emailAddressValidator(String input) {
if (RegExp(r"""^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+.[a-zA-Z]+""")
.hasMatch(input)) return right(input);
return left(const ValueFailures.invalidEmail(
msg: "Invalid Email"));
}
平等的价值对象是:
abstract class ValueObject {
const ValueObject();
Either<ValueFailures, String>? get value;
Either<ValueFailures, String>? get extraValue;
bool isValueValid() => value!.isRight();
bool isExtraValueValid() => extraValue!.isRight();
@override
String toString() => 'ValueObject(value: $value, extraValue: $extraValue)';
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is ValueObject &&
other.value == value &&
other.extraValue == extraValue;
}
@override
int get hashCode => value.hashCode ^ extraValue.hashCode;
}