gpt4 book ai didi

android - 如何在 Flutter 2 中获取 snapshot.error?

转载 作者:行者123 更新时间:2023-12-05 00:11:45 24 4
gpt4 key购买 nike

我将 Flutter 应用程序更新为 Flutter 2,现在当我尝试在 StreamBuider 中获取 snapshot.error 时,我得到了 enter image description here
这些是带有 Streams 的验证器。

class LoginStreams with Validators {

dispose() {
_emailController.close();
_passwordController.close();
}

Function(String) get emailOnChange => _emailController.sink.add;
Function(String) get passwordOnChange => _passwordController.sink.add;

final _emailController = StreamController<String>.broadcast();
final _passwordController = StreamController<String>.broadcast();

Stream<String> get emailStream =>
_emailController.stream.transform(emailValidator);
Stream<String> get passwordStream =>
_passwordController.stream.transform(passwordValidator);

}
--
class Validators {
final passwordValidator = StreamTransformer<String, String>.fromHandlers(
handleData: (password, sink) {
password.length >= 5
? sink.add(password)
: sink.addError("La contraseña debe contener más de 5 caracteres");
});

final emailValidator =
StreamTransformer<String, String>.fromHandlers(handleData: (email, sink) {
if (email.contains('@') && email.contains('.')) {
sink.add(email);
} else {
sink.addError("Ingrese un email válido");
}
});
}
StreamBuilder(
stream: _validators.emailStream,
builder: (BuildContext context, AsyncSnapshot snapshot)=>TextField(
keyboardType: TextInputType.emailAddress,
controller: emailController,
decoration: InputDecoration(
errorText: '$snapshot.error',
labelText: widget.loginModel.emailTextfield,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(CmbSpacing.SpaceMD))),
onChanged: _validators.emailOnChange,
),
),
SizedBox(height: CmbSpacing.SpaceSM),
StreamBuilder(
stream: _validators.passwordStream,
builder: (BuildContext context, AsyncSnapshot snapshot)=>TextField(
controller: passwordController,
obscureText: true,
decoration: InputDecoration(
errorText: '$snapshot.error',
suffixIcon: Icon(Icons.visibility_off_outlined),
labelText: widget.loginModel.passwordTextfield,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(CmbSpacing.SpaceMD))),
onChanged: _validators.passwordOnChange,
),
),
如果有人可以帮助我,我将非常感激,我很困惑,因为在升级到 Flutter 2 之后,它曾经工作得很好。
Flutter 2 中的 StreamBuilders 有什么新东西吗?
编辑:
我将“$snapshot.error”更改为 snapshot.error.toString()
现在我得到空值。
enter image description here

最佳答案

您没有正确插入错误。
如果你想在插值之前调用一个额外的方法,你必须将表达式包装在 ${} 中。 ,例如'${snapshot.error} .在您的情况下,您只需附加 .errorsnapshot 的字符串表示形式
代替

errorText: '$snapshot.error'

errorText: snapshot.error?.toString()

// or if you want to use it in a sentence
errorText: "This is the error: ${snapshot.error}"

关于android - 如何在 Flutter 2 中获取 snapshot.error?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68051711/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com