Flutter StreamBuilder与FutureBuilder [英] Flutter StreamBuilder vs FutureBuilder
问题描述
StreamBuilder
和 FutureBuilder
的主要区别是什么。
-
使用什么以及何时使用?
-
他们打算执行什么任务?
-
每个人如何侦听动态列表中的更改?
StreamBuilder
和 FutureBuilder
具有相同的行为:他们监听各自对象上的更改。并在通知他们
新值时触发新的构建。
因此,最终,他们的区别在于他们侦听对象的方式。 / p>
未来
类似于JS或<$ c中的 Promise
c#中的$ c>任务。它们是异步请求的表示。 未来
只有一个响应。 Future
的常见用法是处理http调用。您可以在未来
上聆听它的状态。无论是完成,成功完成还是出错。
流
就像异步 Iterator
在JS中。可以将其同化为随时间变化的值。它通常是Web套接字或事件(例如点击)的表示。通过收听 Stream
,您将获得每个新值,以及 Stream
是否有错误或已完成。 / p>
他们每个人如何侦听动态列表中的更改?
A 未来
无法收听变量更改。这是一次回应。相反,您需要使用流
。
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?
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 is how the object they listen to works.
Future
are 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 on a Future
is it's state. Whether it's done, finished with a success, or had an error. But that's it.
Stream
on the other hand are 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 click). 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
.
这篇关于Flutter StreamBuilder与FutureBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!