import 'dart:async';
void main() {
var dog = Dog();
showTheDogACat(dog);
print('outside');
dog.bark();
}
class Cat{
void runAway(msg){
print("$msg I'm running away!");
}
}
class Dog{
var _barkController = StreamController();
Stream get onBark => _barkController.stream.asBroadcastStream();
void bark(){
_barkController.add("woof");
}
}
showTheDogACat(dog){
var cat = Cat();
dog.onBark.listen((event)=>cat.runAway(event));
dog.onBark.listen((event)=>print(event)); //why Exception: Stream already has subscriber?
print('inside');
dog.bark();
}
why does the 2nd dog.onBark.listen
call generate Exception: stream already has subscriber? I thought broadcast streams could have many subscribers?