[Dart] Capture and Handle Data Sequences with Streams in Dart
Streams represent a sequence of asynchronous events. Each event is either a data event, also called an element of the stream, or an error event, which is a notification that something has failed. When a stream has emitted all its events, a single "done" event will notify the listener that the end has been reached. In this lesson, we will learn how we can capture and handle streaming data, working with various Stream classes.
// Single Stream
StreamController<String> controller = StreamController<String>();
// Setup listener
controller.stream.listen(
(data) => print('Received data: ${data.toUpperCase()}'),
onDone: () => print('done'),
onError: (e) => print('error $e')
);
// Emit event
controller.add('Hello');
controller.add('World');
// trigger error
controller.addError('Throwing this error');
// trigger done
await controller.close();
print('after done, return future, clean up');
// Broadcast Stream
StreamController<String> controller2 = StreamController<String>();
Stream<String> boradcast = controller2.stream.asBroadcastStream(); boradcast.listen((data) => print('Received data: $data'));
boradcast.listen((data) => print('Received data again: $data')); controller2.add('Hello1');
controller2.add('World2');
// Future-based streams
Future<String> result = HttpRequest.getString('https://swapi.co/api/people/1');
Stream<String> resultStream = Stream.fromFuture(result);
resultStream.listen(
(data) => print('Got data: $data'),
onError: (e) => print(e.type),
onDone: () => print('No more data on stream.')
); // Future-based multi streams
Future<String> result2 = HttpRequest.getString('https://swapi.co/api/people/2');
Stream<String> peopleStream = Stream.fromFutures([result, result2]);
peopleStream.listen(
(person) => print('=> Got person: $person'),
onDone: () => print('No more people on stream.')
);
// Typeahead
List<String> chars = 'Dart is awesome'.split('');
Stream<String> charStream = Stream.fromIterable(chars);
var idx = ;
charStream.listen((char) {
Timer(Duration(milliseconds: idx * ), () => print(char));
idx++;
});
[Dart] Capture and Handle Data Sequences with Streams in Dart的更多相关文章
- [rxjs] Async, handle data over time
If I have an array, and I want to apply filter, map, forEach to it. let Observable = Rx.Observable; ...
- [Recompose] Handle React Events as Streams with RxJS and Recompose
Events are the beginning of most every stream. Recompose provides a createEventHandler function to h ...
- [dart学习]第一篇:windows下安装配置dart编译环境,写出helloworld
前言 博主非科班出身,平时多用C语言,最近想了解学习一门第二语言,看上了可用于移动开发的目前还小众一点dart,准备用一段比较长的时间来慢慢学习.理解. 关于dart语言不再详细介绍了,大家可以访问 ...
- Dart:2.通过一个简单程序来理解Dart基础语法
一 . 一个简单的 Dart 程序 // 这是程序执行的入口. main() { var number = 42; // 定义并初始化一个变量. printNumber(number); // 调用一 ...
- Stream Processing 101: From SQL to Streaming SQL in 10 Minutes
转自:https://wso2.com/library/articles/2018/02/stream-processing-101-from-sql-to-streaming-sql-in-ten- ...
- Visualize real-time data streams with Gnuplot
源文地址 (September 2008) For the last couple of years, I've been working on European Space Agency (ESA) ...
- 使用 empApi 组件实现 Change Data Capture 功能
Change Data Capture 功能是从 Winter '19 版本开始正式启用的功能. 它是基于"发布-订阅"模式设计,可以将 Salesforce 中记录的改变自动推送 ...
- Putting Apache Kafka To Use: A Practical Guide to Building a Stream Data Platform-part 1
转自: http://www.confluent.io/blog/stream-data-platform-1/ These days you hear a lot about "strea ...
- Dart 基础重点截取 Dart 2 20180417
官网教程 https://www.dartlang.org/guides/language/language-tour dart是一个单线程的语言,没有多线程 Final and const If y ...
随机推荐
- (2)Spring Boot配置
文章目录 配置文件 YAML 语法 单元测试 配置文件值自动注入 @Value 获取配置文件属性的值 加载指定配置文件 优先级问题 加载Spring 的配置文件 为容器中添加组件 随机数 & ...
- 欧拉图Eulerian Graph
一.节点的度 无向图:节点的度为该节点所连接的边数 有向图:节点的度分为入度和出度. 二.欧拉图定义 具有欧拉回路的图称作欧拉图,具有欧拉路径而无欧拉回路的图称为半欧拉图. 欧拉回路: 通过图中每 ...
- Cortex_m7内核cache深入了解和应用
一,cache概述 从下图可以看出,从M7内核才开始有的cache,这对于从M0,M3,M4一路走来的小伙伴来说,多了一个cache就多了一个障碍. Cortex-M7 core with 32K/3 ...
- vue基于 element ui 的按钮点击节流
vue的按钮点击节流 场景: 1.在实际使用中,当我们填写表单,点击按钮提交的时候,当接口没返回之前,迅速的点击几次,就会造成多次提交. 2.获取验证码,不频繁的获取. 3.弹幕不能频繁的发 基于这几 ...
- •C#进阶系列——WebApi接口测试工具:WebApiTestClient
阅读目录 一.WebApiTestClient介绍 二.WebApiTestClient展示 三.WebApiTestClient使用 1.如何引入组件 2.如何使用组件 四.总结 正文 前言:这两天 ...
- jquery.marquee
http://aamirafridi.com/jquery/jquery-marquee-plugin#examples <script src="/plugins/marquee/j ...
- Go 编译 && 工具
编译和工具链 Go 的工具链非常丰富,从获取源码.编译.文档.测试.性能分析,到源码格式化.源码提示.重构工具等应有尽有 在 Go 中可以使用测试框架编写单元测试,使用统一的命令行即可测试及输出测试报 ...
- 【转载】Sqlserver使用Group By进行分组并计算每个组的数量
在SQL语句查询中,Group By语句时常用来进行分组操作,有时候在分组的同时还需要计算出每个组的数量多少.在Sqlserver数据库中可以使用Group By加Count聚合函数来实现此功能,即通 ...
- springboot 常见的启动器
<!--pringBoot提供了一个名为spring-boot-starter-parent的工程, 里面已经对各种常用依赖(并非全部)的版本进行了管理 我们的项目需要以这个项目为父工程,这样我 ...
- iptables的nat规则骚操作
水一枪 我对防火墙这块的认知是比较低的, 之前一直没怎么去用 最多的要么就是 iptables -A INPUT -p tcp --dport 80 -j ACCEPT iptables -A OUT ...