我正在Streambuilder
使用BLoC
. 场景是我在我的登录屏幕中,我单击了电子邮件字段并输入了我的电子邮件。我通过单击该字段继续输入密码并输入我的密码。
情况1:如果我在填写完所有字段后立即按下登录按钮而不关闭键盘,则值被存储并继续登录。
案例 2:我在填写完字段后关闭键盘,然后它没有继续登录而是显示错误
The following ValueStreamError was thrown while handling a gesture:
ValueStream has no value. You should check ValueStream.hasValue before accessing ValueStream.value,
or use ValueStream.valueOrNull instead.
Widget loginButton(Orientation orientation) {
return Container(
margin: EdgeInsets.symmetric(
horizontal: orientation == Orientation.landscape
? SizeConfig.widthMultiplier * 3.5
: 0),
padding: EdgeInsets.symmetric(horizontal: SizeConfig.widthMultiplier * 5),
child: StreamBuilder<Object>(
stream: loginBloc.loginCheck,
builder: (context, snapshot) {
return ElevatedButton(
onPressed: () =>
snapshot.hasData ? loginBloc.submit() : _showToast(context),
child: Text(
StringUtil.loginButton,
style: textDefaultStyle(
colorWhite, FontStyle.normal, FontWeight.w600),
),
style: ElevatedButton.styleFrom(
primary: colorPrimary,
fixedSize:
Size(MediaQuery.of(context).size.width, buttonHeight),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(50))),
);
}),
);
}
class LoginBloc extends Object with Validators {
final _emailController = BehaviorSubject<String>();
final _passwordController = BehaviorSubject<String>();
final BuildContext context;
LoginBloc({required this.context});
Function(String) get emailChanged => _emailController.sink.add;
Function(String) get passwordChanged => _passwordController.sink.add;
Stream<String> get email => _emailController.stream.transform(emailValidator);
Stream<String> get password =>
_passwordController.stream.transform(passwordValidator);
Stream<bool> get loginCheck =>
Rx.combineLatest2(email, password, (e, p) => true);
submit() {
APIService.userLogin(
context, _emailController.value, _passwordController.value);
}
void dispose() {
_emailController.close();
_passwordController.close();
}
}