[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 ...
随机推荐
- google test 打印派生类对象
在Unison中使用google test时,发现EXPECT_EQ在fail时,不能打印Unison Test Language中定义的派生类的对象.于是写了个纯C++的示例,发现在只定义基类的op ...
- Filter讲解4
想要 浏览更多Fiddler内容:请点击进入Fiddler官方文档 阅读目录: 一.使用.NET代码扩展Fiddler 二.实现Fiddler接口 三.创建Fiddler扩展项目 四.在扩展程序选项卡 ...
- ALV报表——抓取工单长文
ABAP抓取工单长文 运行效果: 代码: *&---------------------------------------------------------------------* *& ...
- em...刚打完一点cf。。 有点子感悟
首先,下笔一定要读清楚题目. 情况多考虑一下. 这几次的模拟赛,分类思想很重要,往往一大坨东西扔给你,你不去尝试分类的话就很难整理清楚.
- [v]Windows下Git安装指南
参考<Git权威指南>安装整理,图书配套网址参见[1] 1. Cygwin下安装配置Git 1. 在Windows下安装配置Git有2种不同的方案 (1)msysGit, (2)Cygwi ...
- syslog 日志
syslog日志是系统日志的一种,可以存放在本地也可以发送到syslog日志服务器, 但是syslog日志由于的格式不统一,在日常工作中审计syslog日志是一种很麻烦的 事情.不过在2001出现了一 ...
- activemq BytesMessage || TextMessage
需求:使用 python 程序向 activemq 的主题推送数据,默认推送的数据类型是 BytesMessage,java 程序那边接收较为麻烦,改为推送 TextMessage 类型的数据 解决方 ...
- jQuery遍历(1)
jQuery 遍历,意为“移动”,用于根据其相对于其他元素的关系来“查找”(或选取)HTML 元素.以某项选择开始,并沿着这个选择移动,直到抵达您期望的元素为止. 图示解释: 举例: jQuery p ...
- K2 BPM_采购端到端解决方案,激活合规采购新动能_十年专注业务流程管理系统
「方案背景」企业管理标准化演进之路 企业的成长离不开标准化,企业的可持续发展更离不开标准化.随着市场竞争的日趋激烈,标准化已经成为企业参与市场竞争的战略性手段,也成为企业可持续发展的重要手段.聚焦到采 ...
- Innodb学习
一.并发控制 为什么要进行并发控制? 因为并发的任务会对同一个临界资源进行操作,如果不进行并发控制,可能导致不一致,故必须进行并发控制. 如何进行并发控制? 1.锁. 2.数据多版本. 施工ing.. ...