Home > OS >  What are the similarities and differences between Future and Stream in Flutter?
What are the similarities and differences between Future and Stream in Flutter?

Time:06-09

As of my understanding,

  • A Future is used to represent a potential value, or error, that will be available at some time in the future. Receivers of a Future can register callbacks that handle the value or error once it is available.
  • and Streams are part of Dart, and Flutter “inherits” them. and There are two types of streams in Flutter: single subscription streams and broadcast streams. Single subscription streams are the default. They work well when you're only using a particular stream on one screen.

but, what things that are the similarities and differences between them.

CodePudding user response:

Both futures and streams are parts of Dart. The main similarity between them is that they are both used for asynchronous programming. The main difference between them is:

  • a future is used for a value which may be not available right now, may become available at some later moment, and then will not change. We want to react when the value becomes available (and e.g. display it)
  • a stream is used when some value changes over time and we want to react to the changes (e.g. by displaying the current value)

CodePudding user response:

One of the similarities is that both will return in the future(asynchronous). One difference is that Future only returns once. The stream will return again and again.

  • Related