'How to get the snapshot.error in Flutter 2?
I updated my Flutter app to Flutter 2, and now when I try to get the snapshot.error in my StreamBuider I get this
These are validators with 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,
),
),
If someone could help me i would be so gratefull, I am so confused because after upgrading to Flutter 2 this used to work great.
It's something new about StreamBuilders in Flutter 2?
EDIT:
I changed "$snapshot.error" to snapshot.error.toString()
and now I get null.
Solution 1:[1]
You're not interpolating the error correctly.
If you want to call an extra method before interpolating, you have to wrap the expression in ${}
, e.g. '${snapshot.error}
. In your case you're just appending .error
to a string representation of snapshot
Replace
errorText: '$snapshot.error'
With
errorText: snapshot.error?.toString()
// or if you want to use it in a sentence
errorText: "This is the error: ${snapshot.error}"
Solution 2:[2]
You have to apply condition statement overthere:
errorText: snapshot.hasError ? '${snapshot.error}' : null
I'm sure this will help. :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Muhteva |