Future vs. Stream
Explore the fundamentals of asynchronous programming in Dart by understanding futures and streams. This lesson shows how to execute tasks without blocking the user interface by using futures for single delayed results and streams for continuous data. You will learn how to handle asynchronous code effectively in Flutter applications.
In this lesson, we’ll learn about the concept of asynchronous programming. But first, we need to understand what synchronous programming is. In synchronous programming, the programming expression executes line by line. Let’s see an example:
So, in synchronous programming, each statement executes sequentially. So here, statement 1 executes first. Then after that, 2, 3, and so on until we exit our application.
But the problem in synchronous programming occurs if statement 3 somehow takes too much time. Suppose we are doing the add function on a server, so there will be a server response time. Then the application will freeze at statement 3, never proceed, and the user screen will hang. So to avoid that, we use asynchronous programming.
Dart has two features that help us do asynchronous programming called futures and streams.
Futures
Futures are just like its name itself. They will return value, or the function will complete ...