流媒体播种

问题描述 投票:0回答:2
如何创建流并将其与StreamBuilder()一起使用。 我需要实时流式传输外部存储目录,例如AS Firebase Cloud Firestore,如果存储有当前的文件,请获取信息。例如,我有从应用程序下载的视频或图像,并希望收听外部存储目录以实时显示文件。当我删除从屏幕上删除的任何文件时。

flutter dart mobile hybrid-mobile-app mobile-application
2个回答
4
投票
StreamBuilder

小部件对于此类情况确实很有用,您可以检查文档

Herey
。 首先,什么是流?想象一下,它就像管子一样,在管的一侧传递,异步(这意味着并非所有项目都同时传递);而且,在未来的某个时候,这些物品再次到达管子的另一端,异步。 现在,知道什么是Stream

,我们必须意识到我们不知道我们的要求何时结束,它可能永远不会结束!因此,Flutter为我们提供了

StreamBuilder

小部件。假设您的firebase实例返回一个名称

Stream<List<Item>>

。使用
getListOfItems
,您可以做:
StreamBuilder
对于任何基本实施
//... As a child of any of your widgets StreamBuilder( stream: instance.getListOfItems(), // <-- We simply pass the function that returns the stream, Flutter will manage the rest! builder: (ctx, snapshot) { // The snapshot contains the data that is being recieved if any // you can check the docs: // https://api.flutter.dev/flutter/widgets/AsyncSnapshot-class.html // And for the ConnectionState enum // https://api.flutter.dev/flutter/widgets/ConnectionState-class.html if (snapshot.connectionState == ConnectionState.waiting) { // What should be rendered when loading or waiting for a response? return Loading(); } if (snapshot.hasData) { // What should be rendered when there's data? // Remember, data may keep arriving return MyList(snapshot.data); } if (snapshot.hasError) { // Wow, the stream failed, what should be rendered when there's an error? return Error(); } } )
都应该做。您可以从这里构建动态列表,分页等!

将此代码引用bloc

中的流

https://github.com/tejasthonge/flutter-bloc-state-mangement/blob/main/4_streams/_1streas/_1streas/lib/stream1/bloc/bloc/streams_bloc.dart


0
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.