Reputation: 100
In my Flutter app, I use a BehaviorSubject()
to listen to an email text field.
when user successfully log in I called dispose function to close it to avoid memory leaks.
In case of the user signs out and tries to log in again, can I reopen the stream ?
final _emailStream = BehaviorSubject<String>.seeded("");
final validateEmail = StreamTransformer<String, String>.fromHandlers(handleData: (email, sink) {
var isEmailValid = RegExp(
r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+")
.hasMatch(email);
isEmailValid ? sink.add(email) : sink.addError("Enter a valid Email");
});
Stream<String> get email => _emailStream.stream.transform(validateEmail);
void updateEmail(String email) => _emailStream.sink.add(email);
void dispose() {
_emailStream.close();
}
Upvotes: 2
Views: 2285
Reputation: 199
I'm pretty sure you can't re-open a stream.
However, in my current project, I open the stream when I build my login class. So when I login, I dispose my streams, and when I log out the bloc containing my streams is rebuild and new streams are re-created.
Probably not the best way, but it works for me!
Building formbloc containing my streams
Upvotes: 3