What is Streams in Flutter/Dart?

Technology CommunityCategory: FlutterWhat is Streams in Flutter/Dart?
VietMX Staff asked 3 years ago
  • Asynchronous programming in Dart is characterized by the Future and Stream classes.
  • stream is a sequence of asynchronous events. It is like an asynchronous Iterable—where, instead of getting the next event when you ask for it, the stream tells you that there is an event when it is ready.
  • Streams can be created in many ways but they all are used in the same way; the asynchronous for loopawait for). E.g
	Future<int> sumStream(Stream<int> stream) async {
	  var sum = 0;
	  await for (var value in stream) {
	    sum += value;
	  }
	  return sum;
	}
  • Streams provide an asynchronous sequence of data.
  • Data sequences include user-generated events and data read from files.
  • You can process a stream using either await for or listen() from the Stream API.
  • Streams provide a way to respond to errors.
  • There are two kinds of streams: single subscription or broadcast.