Last active
August 12, 2022 07:13
-
-
Save rileyseaburg/a9674f5e365b86dcc52c104f4a5b64ac to your computer and use it in GitHub Desktop.
Dart Stream Getters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
/// Demontrates how to conviently access stream data with Getters. | |
void main() { | |
final bloc = Bloc(); | |
/// Manually update from stream | |
// bloc.emailController.sink.add("my new email"); | |
/// Listen for the email to change and print the value. | |
/// Add type annotation to the method | |
bloc.email.listen((value) { | |
print(value); | |
}); | |
/// Use Stream Getter | |
bloc.changeEmail("My new email"); | |
} | |
class Bloc { | |
/// Mark private with '_' | |
final _emailController = StreamController<String>(); | |
final passwordController = StreamController<String>(); | |
/// Add type annotation to the getter. | |
Function(String) get changeEmail => emailController.sink.add; | |
Function(String) get changePassword => passwordController.sink.add; | |
// get access to email address through the stream with type annotation | |
Stream<String> get email => emailController.stream; | |
// get access to password through the stream with type annotation | |
Stream<String> get password => passwordController.stream; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment