[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 ...
随机推荐
- 【坑】关于springMvc对JSON日期绑定,得到的日期后面多个时间的问题
文章目录 前言 Mysql的Date() 后记 前言 当我们翻过 解决springMvc对JSON日期绑定 眼前这座大山以后,发现并没有 IG 的荣光在等着我们,反而有个大坑在等着我们.... 比如博 ...
- 创建线程的三种方式(Thread、Runnable、Callable)
方式一:继承Thread类实现多线程: 1. 在Java中负责实现线程功能的类是java.lang.Thread 类. 2. 可以通过创建 Thread的实例来创建新的线程. 3. 每个线程都是通过某 ...
- Linux shell 中 & && [] [[]] () [] 含义
| 语法:command 1 | command 2 功能:把第一个命令 command 1 执行的结果作为 command 2 的输入传给 command 2 & & 放在启动参数后 ...
- 【程序人生】程序员真会玩,工作996,生病ICU
昨天Github上一个项目彻底爆红了网络,短短一天star数突破一万,Issues已破1800,大家纷纷说出有关企业的不合理加班行为,句句吐露程序员的心声,掀起了一波抵制加班潮,抵制996. 该项目里 ...
- 如何在mongoengine中使用referencefield引用本类
引用:原文 from mongoengine import * class Employee(Document): name = StringField() boss = ReferenceField ...
- Android去评分,分享
去评分: 跳到手机中已安装的市场评分页面 Uri uri = Uri.parse("market://details?id="+getPackageName()); Intent ...
- Authentication源码解析
1.获取当前的 Subject. 调用 SecurityUtils.getSubject(); 从当前线程的threadLocals属性中获取Subject对象 SecurityUtils publi ...
- Spring Cloud Alibaba学习笔记(2) - Nacos服务发现
1.什么是Nacos Nacos的官网对这一问题进行了详细的介绍,通俗的来说: Nacos是一个服务发现组件,同时也是一个配置服务器,它解决了两个问题: 1.服务A如何发现服务B 2.管理微服务的配置 ...
- Access-Control-Max-Age
app.UseCors(builder => builder .AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader() .AllowCreden ...
- jQuery AJAX基础
一.JSON 定义: JSON(JavaScript Object Notation, JS对象标记)是一种轻量级的数据交换格式. 它基于 ECMAScript ...