https://github.com/dulajkavinda/rxdart_demo-ft
Building a flutter application to understand different types on subject streams in rxdart library.
https://github.com/dulajkavinda/rxdart_demo-ft
dart flutter rxdart
Last synced: 8 months ago
JSON representation
Building a flutter application to understand different types on subject streams in rxdart library.
- Host: GitHub
- URL: https://github.com/dulajkavinda/rxdart_demo-ft
- Owner: dulajkavinda
- Created: 2020-12-19T07:04:27.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-12-20T23:02:01.000Z (almost 5 years ago)
- Last Synced: 2025-03-27T08:53:35.051Z (8 months ago)
- Topics: dart, flutter, rxdart
- Language: Dart
- Homepage: https://pub.dev/documentation/rxdart/latest/
- Size: 59.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Understanding RXDart Streams
### Types of subjects in RxDart
---
| Standard Subject | Publisher Subject |
| :--- | :----: |
| The base for all Subjects. If you'd like to create a new Subject, extend from this class. | Exactly like a normal broadcast StreamController with one exception: this class is both a Stream and Sink. |
| Behaviour Subject | Replay Subject |
| :--- | :----: |
| A special StreamController that captures the latest item that has been added to the controller, and emits that as the first item to any new listener. | A special StreamController that captures all of the items that have been added to the controller, and emits those as the first items to any new listener. |
## Implementation
---
1. Build a BLoC class and create four subject streams
```dart
final _dartStream = StreamController();
final _publishStream = PublishSubject();
final _behaviourStream = BehaviorSubject();
final _replayStream = ReplaySubject();
```
2. Write the main function that calls from the widget, for this application its **startReading()**
```dart
startReading() async {
Future loadAsset() async {
return await rootBundle.loadString('assets/sonnet.txt');
}
var contents = loadAsset();
contents.then((value) {
print(value);
});
}
```
3. Wrap the main method with **Provider** class
```dart
return Provider(
create: (context) => ReaderBloc(),
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.red,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: Launch(),
),
);
```
4. Then create a **ReaderBloc** instance in the widget
```dart
readerBloc = Provider.of(context);
```
5. Call the method using the **bloc** instance
```dart
readerBloc.startReading();
```
## Notes
---
All of the RxDart streams are multiple subscription, but StreamController is single subscription. So you get to listen once to the stream. So if you want to listing with multiple subscriptions you need to add **asBroadcastStream** to the **StreamController.**
```dart
Stream get dartStream => _dartStream.stream.asBroadcastStream();
```
## How it all Works?
---
**BehaviorSubject** remembers the previous value and get that value.
**PubishSubject** does't get the current value, instead it gets the value to come down next. This is exactly like a broadcast **StreamController**
**ReplaySubject** tracks all history of value that is added to the stream. We can cap the values that kept in the stream.