Single subscription vs broadcast streams

Andrea Bizzotto
A free video tutorial from Andrea Bizzotto
Flutter GDE, Developer and Instructor
4.6 instructor rating • 4 courses • 30,390 students

Lecture description

Learn more from the full course

Flutter & Firebase: Build a Complete App for iOS & Android

Learn Dart, Flutter & Firebase by Google, and build a real-world, production ready app

20:46:18 of on-demand video • Updated May 2021

  • Build a complete, real-world app with Flutter & Firebase
  • Write iOS & Android apps with a single codebase
  • Write production-ready code following best practices and become a competent Flutter developer
  • Fast-track your Flutter learning with practical tips and proven techniques
English [Auto] In the last few videos we have talked about observer goals and how we can combine multiple stream with Iraq's start. However this section about advance to stream operations is not complete until we talk about single and broadcast streams and what primitives are available in Iraq start to work with broadcast streams and to talk about this subject. We can go back to the basics. So what I have over here is this dark but with a very simple program that creates a stream controller and then it tries to listen to this controller twice. And if I run this code I get an exception telling me that the stream has already been listened to and the reason is that when we create upstream controller like this then we are creating a single subscription stream. And this means that we can't subscribe to that stream more than once. So in practice if you have a single subscription stream that is your source of data then you can't have two separate widgets that listen to it for example by a stream bidders. And it is very easy to forget this especially when you work on bigger apps where streams and those subscriptions may be created in different parts of the code base. So if you need multiple listeners then you had to create a broadcast stream and you can do that by typing stream controller without broadcast like this. And if I run this code then I can see that the exception disappears. So let me say this once again if you want our stream to have more than one listener then you need to create a broadcast stream. And now that we have this we should try to understand what happens when we add values to this thing. So what I'm going to do now is to had three values to this controller. So here I can type the controller the other with the value of before and then between the two listeners I can add another line that says controller dot ad with between and at the end I can have another one that says controller that ad with after and now if I run this code I can see in the console log that the first value called before is never printed and this is because these listeners are only created after this value is added and in fact the first line that is printed says Listener 1 with between and this happens because we that this value after the first place and then was created. And finally the after value is printed twice because by the time this is added both listeners are active. So there is one very important takeaway here and that is that listeners will only receive events from the moment they start listening to the string. Okay. So in summary we should now understand that difference between single subscription and broadcast streams and that listeners will only receive events that are added after they start listening to the string. So let's continue on the next video.