Created
July 4, 2021 06:41
-
-
Save achinverma/b0e4c97ffbcc171fc0c56f2db8926edc to your computer and use it in GitHub Desktop.
This file contains 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
enum PostBlocAction {FetchPosts} | |
class PostBloc{ | |
//This StreamController is used to update the state of widgets | |
PublishSubject<PostResponse> _stateStreamController = new PublishSubject(); | |
StreamSink<PostResponse> get _postSink => _stateStreamController.sink; | |
Stream<PostResponse> get postStream => _stateStreamController.stream; | |
//user input event StreamController | |
PublishSubject<PostBlocAction> _eventStreamController = new PublishSubject(); | |
StreamSink<PostBlocAction> get eventSink => _eventStreamController.sink; | |
Stream<PostBlocAction> get _eventStream => _eventStreamController.stream; | |
PostBloc() { | |
_eventStream.listen((event) async { | |
if (event == PostBlocAction.FetchPosts){ | |
PostResponse postModel = await ApiController.getPostsData(); | |
_postSink.add(postModel); | |
} | |
}); | |
} | |
void dispose(){ | |
_stateStreamController.close(); | |
_eventStreamController.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment