Stream Subscription Get Result In Flutter

Stream Subscription Get Result In Flutter :

Stream Subscription Get Result In Flutter
main(){
  Stream<String> stream = new Stream.fromFuture(getData());

  stream.listen((data) {
    print("DataReceived: "+data);
  }, onDone: () {
    print("Task Done");
  }, onError: (error) {
    print("Some Error");
  });
}

Future<String> getData() async {
  await Future.delayed(Duration(seconds: 5)); //Mock delay
  print("Fetched Data");
  return "This stream subscribe";
}

StreamController subscription :

main() {
  StreamController<String> streamController = new StreamController();

  streamController.stream.listen((data) {
    print("DataReceived: " + data);
  }, onDone: () {
    print("Task Done");
  }, onError: (error) {
    print("Some Error");
  });

  streamController.add("This a StreamController subscribe");
  streamController.close();
}

Broadcast Streams (single-subscription streams and broadcast streams) :

main() {
  StreamController<String> streamController = new StreamController.broadcast();
  //First subscription
  streamController.stream.listen((data) {
    print("Data Received1: " + data);
  }, onDone: () {
    print("Task Done1");
  }, onError: (error) {
    print("Some Error1");
  });

  //Second subscription
  streamController.stream.listen((data) {
    print("Data Received2: " + data);
  }, onDone: () {
    print("Task Done2");
  }, onError: (error) {
    print("Some Error2");
  });

  streamController.add("This a test data");
}

The flutter tutorial  is a website that bring you the latest and amazing resources of code. All the languages codes are included in this website. The languages like flutter, android, java,kotlin etc.with the help of this languages any user can develop the beautiful application

For more information about Flutter. visit www.fluttertutorial.in