Architecting Bloc and StreamControllers to handle events
I'm building a Flutter app using BLoCs. I have a view where I send multiple events to the bloc and also streams data to the view.
Currently, I have different sinks I'm adding to depending on what the user is doing e.g. clicking a radio button, clicking a button to increase a counter, all in the same view. I'm searching how to handle these events in a common sink as it seems like a lot of plumbing to have a sink for each part. When I'm changing state I use different streams it's, but it's for the event handling part I'm looking around for a solution.
Any guidance to make the sink handling cleaner or is this the way to do it?
example of the current code I would like to streamline
// distance
StreamController<bool> _cmdDistanceController = StreamController<bool>();
StreamSink get distanceCommands => _cmdDistanceController.sink;
StreamController<String> _syncDistanceController =
StreamController<String>.broadcast();
Stream<String> get syncDistances => _syncDistanceController.stream;
// speed
StreamController<bool> _cmdSpeedController = StreamController<bool>();
StreamSink get speedCommands => _cmdSpeedController.sink;
StreamController<String> _syncSpeedController =
StreamController<String>.broadcast();
Stream<String> get syncSpeed => _syncSpeedController.stream;
flutter
|
show 2 more comments
I'm building a Flutter app using BLoCs. I have a view where I send multiple events to the bloc and also streams data to the view.
Currently, I have different sinks I'm adding to depending on what the user is doing e.g. clicking a radio button, clicking a button to increase a counter, all in the same view. I'm searching how to handle these events in a common sink as it seems like a lot of plumbing to have a sink for each part. When I'm changing state I use different streams it's, but it's for the event handling part I'm looking around for a solution.
Any guidance to make the sink handling cleaner or is this the way to do it?
example of the current code I would like to streamline
// distance
StreamController<bool> _cmdDistanceController = StreamController<bool>();
StreamSink get distanceCommands => _cmdDistanceController.sink;
StreamController<String> _syncDistanceController =
StreamController<String>.broadcast();
Stream<String> get syncDistances => _syncDistanceController.stream;
// speed
StreamController<bool> _cmdSpeedController = StreamController<bool>();
StreamSink get speedCommands => _cmdSpeedController.sink;
StreamController<String> _syncSpeedController =
StreamController<String>.broadcast();
Stream<String> get syncSpeed => _syncSpeedController.stream;
flutter
"or is this the way to do it"
- post some code of"this"
– pskink
Nov 21 '18 at 14:51
I've updated with some code. it's the _cmdXXX controllers I would like to merge into a single controller in some smart way. Besides what you see above I have 3 more for other inputs in the same wiew
– Rasmus Christensen
Nov 21 '18 at 14:55
hmmm, i dont quite understand what you mean by "merge" but maybe itsStreamGroup
you are looking for? the docs say: "A collection of streams whose events are unified and sent through a central stream."
– pskink
Nov 21 '18 at 14:59
Ohhh haven't heard of StreamGroup, but yes central is what I'm looking for. A central sink I can use instead of exposing two. This would require I add something else than bool
– Rasmus Christensen
Nov 21 '18 at 15:02
a stream ID (indicating the type of command) or something?
– pskink
Nov 21 '18 at 15:04
|
show 2 more comments
I'm building a Flutter app using BLoCs. I have a view where I send multiple events to the bloc and also streams data to the view.
Currently, I have different sinks I'm adding to depending on what the user is doing e.g. clicking a radio button, clicking a button to increase a counter, all in the same view. I'm searching how to handle these events in a common sink as it seems like a lot of plumbing to have a sink for each part. When I'm changing state I use different streams it's, but it's for the event handling part I'm looking around for a solution.
Any guidance to make the sink handling cleaner or is this the way to do it?
example of the current code I would like to streamline
// distance
StreamController<bool> _cmdDistanceController = StreamController<bool>();
StreamSink get distanceCommands => _cmdDistanceController.sink;
StreamController<String> _syncDistanceController =
StreamController<String>.broadcast();
Stream<String> get syncDistances => _syncDistanceController.stream;
// speed
StreamController<bool> _cmdSpeedController = StreamController<bool>();
StreamSink get speedCommands => _cmdSpeedController.sink;
StreamController<String> _syncSpeedController =
StreamController<String>.broadcast();
Stream<String> get syncSpeed => _syncSpeedController.stream;
flutter
I'm building a Flutter app using BLoCs. I have a view where I send multiple events to the bloc and also streams data to the view.
Currently, I have different sinks I'm adding to depending on what the user is doing e.g. clicking a radio button, clicking a button to increase a counter, all in the same view. I'm searching how to handle these events in a common sink as it seems like a lot of plumbing to have a sink for each part. When I'm changing state I use different streams it's, but it's for the event handling part I'm looking around for a solution.
Any guidance to make the sink handling cleaner or is this the way to do it?
example of the current code I would like to streamline
// distance
StreamController<bool> _cmdDistanceController = StreamController<bool>();
StreamSink get distanceCommands => _cmdDistanceController.sink;
StreamController<String> _syncDistanceController =
StreamController<String>.broadcast();
Stream<String> get syncDistances => _syncDistanceController.stream;
// speed
StreamController<bool> _cmdSpeedController = StreamController<bool>();
StreamSink get speedCommands => _cmdSpeedController.sink;
StreamController<String> _syncSpeedController =
StreamController<String>.broadcast();
Stream<String> get syncSpeed => _syncSpeedController.stream;
flutter
flutter
edited Nov 21 '18 at 14:53
Rasmus Christensen
asked Nov 21 '18 at 14:34
Rasmus ChristensenRasmus Christensen
5,12773261
5,12773261
"or is this the way to do it"
- post some code of"this"
– pskink
Nov 21 '18 at 14:51
I've updated with some code. it's the _cmdXXX controllers I would like to merge into a single controller in some smart way. Besides what you see above I have 3 more for other inputs in the same wiew
– Rasmus Christensen
Nov 21 '18 at 14:55
hmmm, i dont quite understand what you mean by "merge" but maybe itsStreamGroup
you are looking for? the docs say: "A collection of streams whose events are unified and sent through a central stream."
– pskink
Nov 21 '18 at 14:59
Ohhh haven't heard of StreamGroup, but yes central is what I'm looking for. A central sink I can use instead of exposing two. This would require I add something else than bool
– Rasmus Christensen
Nov 21 '18 at 15:02
a stream ID (indicating the type of command) or something?
– pskink
Nov 21 '18 at 15:04
|
show 2 more comments
"or is this the way to do it"
- post some code of"this"
– pskink
Nov 21 '18 at 14:51
I've updated with some code. it's the _cmdXXX controllers I would like to merge into a single controller in some smart way. Besides what you see above I have 3 more for other inputs in the same wiew
– Rasmus Christensen
Nov 21 '18 at 14:55
hmmm, i dont quite understand what you mean by "merge" but maybe itsStreamGroup
you are looking for? the docs say: "A collection of streams whose events are unified and sent through a central stream."
– pskink
Nov 21 '18 at 14:59
Ohhh haven't heard of StreamGroup, but yes central is what I'm looking for. A central sink I can use instead of exposing two. This would require I add something else than bool
– Rasmus Christensen
Nov 21 '18 at 15:02
a stream ID (indicating the type of command) or something?
– pskink
Nov 21 '18 at 15:04
"or is this the way to do it"
- post some code of "this"
– pskink
Nov 21 '18 at 14:51
"or is this the way to do it"
- post some code of "this"
– pskink
Nov 21 '18 at 14:51
I've updated with some code. it's the _cmdXXX controllers I would like to merge into a single controller in some smart way. Besides what you see above I have 3 more for other inputs in the same wiew
– Rasmus Christensen
Nov 21 '18 at 14:55
I've updated with some code. it's the _cmdXXX controllers I would like to merge into a single controller in some smart way. Besides what you see above I have 3 more for other inputs in the same wiew
– Rasmus Christensen
Nov 21 '18 at 14:55
hmmm, i dont quite understand what you mean by "merge" but maybe its
StreamGroup
you are looking for? the docs say: "A collection of streams whose events are unified and sent through a central stream."– pskink
Nov 21 '18 at 14:59
hmmm, i dont quite understand what you mean by "merge" but maybe its
StreamGroup
you are looking for? the docs say: "A collection of streams whose events are unified and sent through a central stream."– pskink
Nov 21 '18 at 14:59
Ohhh haven't heard of StreamGroup, but yes central is what I'm looking for. A central sink I can use instead of exposing two. This would require I add something else than bool
– Rasmus Christensen
Nov 21 '18 at 15:02
Ohhh haven't heard of StreamGroup, but yes central is what I'm looking for. A central sink I can use instead of exposing two. This would require I add something else than bool
– Rasmus Christensen
Nov 21 '18 at 15:02
a stream ID (indicating the type of command) or something?
– pskink
Nov 21 '18 at 15:04
a stream ID (indicating the type of command) or something?
– pskink
Nov 21 '18 at 15:04
|
show 2 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414394%2farchitecting-bloc-and-streamcontrollers-to-handle-events%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53414394%2farchitecting-bloc-and-streamcontrollers-to-handle-events%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
"or is this the way to do it"
- post some code of"this"
– pskink
Nov 21 '18 at 14:51
I've updated with some code. it's the _cmdXXX controllers I would like to merge into a single controller in some smart way. Besides what you see above I have 3 more for other inputs in the same wiew
– Rasmus Christensen
Nov 21 '18 at 14:55
hmmm, i dont quite understand what you mean by "merge" but maybe its
StreamGroup
you are looking for? the docs say: "A collection of streams whose events are unified and sent through a central stream."– pskink
Nov 21 '18 at 14:59
Ohhh haven't heard of StreamGroup, but yes central is what I'm looking for. A central sink I can use instead of exposing two. This would require I add something else than bool
– Rasmus Christensen
Nov 21 '18 at 15:02
a stream ID (indicating the type of command) or something?
– pskink
Nov 21 '18 at 15:04