Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter StreamBuilder vs FutureBuilder

What is the main difference between StreamBuilder and FutureBuilder.

  • What to use and when to use?

  • What are the tasks they are intended to perform?

  • How each of them listens to changes in a dynamic list?

like image 703
Prasanth Kanna Avatar asked Jun 13 '18 19:06

Prasanth Kanna


People also ask

What is the difference between StreamBuilder and FutureBuilder in Flutter?

FutureBuilder solves a square value and returns the result after 5 seconds, till then we show a progress indicator to the user. StreamBuilder shows a stopwatch, incrementing _count value by 1 every second.

When should I use StreamBuilder Flutter?

The StreamBuilder widget is used in many kinds of Flutter applications, especially chat applications, social networks, real-time content updates, etc. In this article, we will go over 2 complete examples of implementing StreamBuilder: the first example is a real-time clock app and the second one is a demo chat app.

When should I use a FutureBuilder?

It is necessary for Future to be obtained earlier either through a change of state or change in dependencies. FutureBuilder is a Widget that will help you to execute some asynchronous function and based on that function's result your UI will update.

What does StreamBuilder do in Flutter?

The StreamBuilder can listen to exposed streams and return widgets and catch snapshots of got stream information. The stream builder takes two contentions. The Stream resembles a line. At the point when you enter a value from one side and a listener from the opposite side, the listener will get that value.


1 Answers

Both StreamBuilder and FutureBuilder have the same behavior: They listen to changes on their respective object. And trigger a new build when they are notified of a new value.

So in the end, their differences are how the object they listen to works.

Future is like Promise in JS or Task in c#. They are the representation of an asynchronous request. Futures have one and only one response. A common usage of Future is to handle HTTP calls. What you can listen to on a Future is its state. Whether it's done, finished with success, or had an error. But that's it.

Stream on the other hand is like async Iterator in JS. This can be assimilated to a value that can change over time. It usually is the representation of web-sockets or events (such as clicks). By listening to a Stream you'll get each new value and also if the Stream had an error or completed.

How each of them listens to changes in a dynamic list?

A Future can't listen to a variable change. It's a one-time response. Instead, you'll need to use a Stream.

like image 164
Rémi Rousselet Avatar answered Sep 28 '22 16:09

Rémi Rousselet