正在从flutter中的EventSink中删除错误



我从flutter应用程序中得到了奇怪的行为,试图在两天内解决这个问题。。。

  1. 如果我试图提交登录表单,请在热重启后立即提交(空字段(->错误:字段为空->然后我填写了表格,我会再次尝试重新提交,一切都很好
  2. 如果我在没有提交空表单的情况下重新启动并填写登录表单,则提交按钮将永远不会将数据发送到服务器。由于现有的EventSink错误,由于某些原因,这些错误在第一种情况下不会显示

以下是代码:表单小部件:

@override
Widget build(BuildContext context) {
final FormBloc formBloc = FormProvider.of(context);
Widget _emailField(FormBloc formBloc) {
return StreamBuilder(
stream: formBloc.email,
builder: (context, AsyncSnapshot<String> snapshot) {
return TextFormField(
autofocus: false,
keyboardType: TextInputType.emailAddress,
onChanged: formBloc.changeEmail,

Widget _passwordField(FormBloc formBloc) {
return StreamBuilder(
stream: formBloc.password,
builder: (context, AsyncSnapshot<String> snapshot) {
return TextFormField(
autofocus: false,
obscureText: !_passwordVisible,
keyboardType: TextInputType.text,
onChanged: formBloc.changePassword,

Formbloc:

class FormBloc with LoginValidationMixin {
final _email = BehaviorSubject<String>();
final _password = BehaviorSubject<String>();
final _errorMessage = BehaviorSubject<String>();
// getters
Function(String) get changeEmail => _email.sink.add;
Function(String) get changePassword => _password.sink.add;
Function(String) get addError => _errorMessage.sink.add;
// streams
Stream<String> get email => _email.stream.transform(validatorEmail);
Stream<String> get password => _password.stream.transform(validatorPassword);
Stream<String> get errorMessage => _errorMessage.stream;
Stream<bool> get submitValidForm => Rx.combineLatest3(
email,
password,
errorMessage,
(e, p, er) => true,
);
late AuthService authInfo;
// login
dynamic login(BuildContext context) async {
authInfo = AuthService();
final email = _email.valueOrNull;
final password = _password.valueOrNull;
if (email == null || password == null) {
addError("Password or Email cannot be empty");
return;
}
final response =
await authInfo.login(email, password, this);
if (response == null) {return;}
final data = jsonDecode(response) as Map<String, dynamic>;
if (data['status_code'] != 200) {
} else {
final tokens = data['tokens'];
AuthService.setToken(
tokens["access"], tokens["refresh"], data['client_id']);
Navigator.pushNamed(context, '/home');
return data;
}
}

验证


class LoginValidationMixin {
static String get passwordPattern => "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{8,}";
final validatorEmail = StreamTransformer<String, String>.fromHandlers(
handleData: (email, sink) {
sink.add(email);
if (!EmailValidator.validate(email)) {
sink.addError('Email should look like:'
'welcome@itsy.extn');
} else {
sink.add(email);
}
},
);
final validatorPassword = StreamTransformer<String, String>.fromHandlers(
handleData: (password, sink) {
sink.add(password);
if (!RegExp(passwordPattern).hasMatch(password)) {
sink.addError('A hint, password should containn'
'at least 8 charactersn'
'at least 1 upper case charactern'
'at least 1 upper case charactern'
'at least 1 numerical charactern');
} else {
sink.add(password);
}
},
);
}

登录按钮

Widget _loginButton(FormBloc formBloc) {
return StreamBuilder<bool>(
stream: formBloc.submitValidForm,
builder: (context, AsyncSnapshot<bool> snapshot) {
return Material(
child: MaterialButton(
onPressed: () {
if (snapshot.error != null) {
print(snapshot.error)  <<<< in the second scenario, here is always an error, even if filed validations have passed. While in the first scenario, this does work as expected.
return;
} else {
formBloc.login(context);
}
},

所以,在第二种情况下,EventSink总是保存一个错误,并且按钮基本上是不可点击的。然而,在第一种情况下,EventSink工作正常,如果存在错误,则保留错误,如果添加了正确的值,则清除错误。我该怎么办?是否有清除EventSink错误的方法?

所以这就是我解决这个问题的方法,首先我意识到流只会产生值,而不会存储。

pskink:EvensSink不包含错误接下来,我检查了

Stream<bool> get submitValidForm => Rx.combineLatest3(
email,
password,
errorMessage, <<< this one is null in the case two, therefore my solution was to give it an initial value while handling email or password.
(e, p, er) => true,
);

相关内容

  • 没有找到相关文章

最新更新