Flutter 文本验证程序无法使用bloc和冻结

Flutter 文本验证程序无法使用bloc和冻结,flutter,dart,Flutter,Dart,使用DDD模式验证电子邮件地址不起作用,我使用dartz、bloc和freezed来验证。 我曾试图发现错误,但我做不到 电子邮件地址为: class _EmailAdressInput extends _ListOfInputs { const _EmailAdressInput(); @override Widget build(BuildContext context) { return BlocBuilder<AuthBloc, AuthBlocState>

使用DDD模式验证电子邮件地址不起作用,我使用dartz、bloc和freezed来验证。 我曾试图发现错误,但我做不到

电子邮件地址为:

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"));
}
使用冻结库的状态为:

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"));
}
@冻结
带有$AuthBlocState的抽象类AuthBlocState{
常量工厂AuthBlocState({
所需密码,
提交所需的bool,
必需的bool消息,
必需的选项AuthFailurerSuccessOption,
})=\u AuthBlocState;
工厂AuthBlocState.initial()=>AuthBlocState(
电子邮件:电子邮件地址(“”),
提交的问题:错误,
错误消息:错误,
AuthFailurerSuccessOption:无(),
);
}
集团是:

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"));
}
@覆盖
流映射事件状态(
AuthBloceEvent事件,
)异步*{
yield*event.map(
emailAddressEvent:(e)异步*{
屈服状态.copyWith(
电子邮件:EmailAddress(e.EmailAddressSeventValue),
我的意思是:是的,
AuthFailurerSuccessOption:无(),
);
},
okPressedEvent:(e)异步*{
//得到
//要么我们应该选择
//要检查其中一个值
//我们必须知道这一点
//两者都是对的
if(state.email.isValueValid()
) {
屈服状态.copyWith(
提交的问题:正确,
AuthFailurerSuccessOption:无(),
);
}
},
);}
验证程序是:

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"));
}
任一emailAddressValidator(字符串输入){
如果(RegExp(r“”)^[a-zA-Z0-9.a-zA-Z0-9.!\$%&'*+-/=?^ `{124;}~]+@[a-zA-Z0-9]+\[a-zA-Z]+'')
.hasMatch(输入))返回权(输入);
返回左侧(const ValueFailures.invalidEmail(
消息:“无效电子邮件”);
}
相等的is值对象为:

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;
  
}
抽象类ValueObject{
常量ValueObject();
要么?获得价值;
要么?获得额外价值;
bool isValueValid()=>value!.isRight();
bool isExtraValueValid()=>extraValue!.isRight();
@凌驾
String-toString()=>'ValueObject(值:$value,extraValue:$extraValue)';
@凌驾
布尔运算符==(对象其他){
如果(相同(此,其他))返回true;
返回另一个是ValueObject&&
other.value==值&&
other.extraValue==extraValue;
}
@凌驾
int get hashCode=>value.hashCode^extraValue.hashCode;
}

您没有将
EmailAddressEvent
映射到您的集团中。另外,您不需要使用
context.read().state
当您在
blocBuilder
中时,您只需使用state即可。

我正在应用程序中映射EmailAddressEvent,但我忘了在此处键入它!,我加上去了。谢谢你通知我