what are streams in Dart

What is the difference between async and streams, where we should use streams instead of async, in Dart language.As descried in the official documentation stream represents the sequence of data.

1 answer

  • answered 2018-04-17 05:40 Günter Zöchbauer

    Async execution is registering a callback that is called when some other computation completes. This can be a an operating system like file.readAsString() , or an HTTP request to a server where the client continues executing UI rendering (or other things) and when the response from the server arrives, your code gets called to process the response.

    In Dart you usually get a Future back from such async call, where you can register a callback using .then(/* pass callback here */).

    async and await is syntactic sugar so you don't need to clutter your code with .then(...).then(...).

    A stream can be sync or async. A stream can be seen as iterable like an array, but where the items are pushed instead of pulled.

    An async execution completes a Future when it's done and that was it. A stream can emit a limited or unlimited number of items (events). Like for example if you subscribe (listen) to mousemove in the browser you get mouse move events whenever the mouse moves, until you cancel the subscriptions. With listen(...) you can register a callback that is called for every such event.