Search⌘ K
AI Features

Future vs. Stream

Explore asynchronous programming in Flutter by understanding how Futures provide single delayed results and Streams deliver continuous data. Learn to use async, await, and stream generators to manage data efficiently without freezing your app's interface.

We'll cover the following...

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:

Shell
1. var a = 1;
2. var b = 2;
3. var ans = add(a,b);
4. print(ans);

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 the execution in the ...