Flutter StreamBuilder 与 FutureBuilder [英] Flutter StreamBuilder vs FutureBuilder

查看:24
本文介绍了Flutter StreamBuilder 与 FutureBuilder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

StreamBuilderFutureBuilder 之间的主要区别是什么.

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?

推荐答案

StreamBuilderFutureBuilder 都有相同的行为:它们监听各自对象的变化.并在收到通知时触发新构建具有新价值.

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 就像 JS 中的 Promise 或 C# 中的 Task.它们是异步请求的表示.Futures 只有一个响应.Future 的一个常见用法是处理 HTTP 调用.您可以在 Future 上听到的是它的状态.无论是完成、成功完成还是出现错误.但仅此而已.

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 就像 JS 中的异步 Iterator.这可以被同化为一个可以随时间变化的值.它通常是网络套接字或事件(例如点击)的表示.通过收听 Stream,您将获得每个新值,以及 Stream 是否有错误或已完成.

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?

Future 无法监听变量变化.这是一次性响应.相反,您需要使用 Stream.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆