Skip to content

Instantly share code, notes, and snippets.

@achinverma
Created July 4, 2021 06:41
Show Gist options
  • Save achinverma/b0e4c97ffbcc171fc0c56f2db8926edc to your computer and use it in GitHub Desktop.
Save achinverma/b0e4c97ffbcc171fc0c56f2db8926edc to your computer and use it in GitHub Desktop.
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