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的更多相关文章

  1. [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; ...

  2. [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 ...

  3. [dart学习]第一篇:windows下安装配置dart编译环境,写出helloworld

    前言 博主非科班出身,平时多用C语言,最近想了解学习一门第二语言,看上了可用于移动开发的目前还小众一点dart,准备用一段比较长的时间来慢慢学习.理解. 关于dart语言不再详细介绍了,大家可以访问  ...

  4. Dart:2.通过一个简单程序来理解Dart基础语法

    一 . 一个简单的 Dart 程序 // 这是程序执行的入口. main() { var number = 42; // 定义并初始化一个变量. printNumber(number); // 调用一 ...

  5. 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- ...

  6. Visualize real-time data streams with Gnuplot

    源文地址 (September 2008) For the last couple of years, I've been working on European Space Agency (ESA) ...

  7. 使用 empApi 组件实现 Change Data Capture 功能

    Change Data Capture 功能是从 Winter '19 版本开始正式启用的功能. 它是基于"发布-订阅"模式设计,可以将 Salesforce 中记录的改变自动推送 ...

  8. 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 ...

  9. Dart 基础重点截取 Dart 2 20180417

    官网教程 https://www.dartlang.org/guides/language/language-tour dart是一个单线程的语言,没有多线程 Final and const If y ...

随机推荐

  1. php实现映射

    目录 映射 实现 链表实现: 二叉树实现 复杂度分析 映射 映射,或者射影,在数学及相关的领域经常等同于函数.基于此,部分映射就相当于部分函数,而完全映射相当于完全函数. 映射(Map)是用于存取键值 ...

  2. PAT(B) 1094 谷歌的招聘(Java)

    题目链接:1094 谷歌的招聘 (20 point(s)) 题目描述 2004 年 7 月,谷歌在硅谷的 101 号公路边竖立了一块巨大的广告牌(如下图)用于招聘.内容超级简单,就是一个以 .com ...

  3. 机器学习SVD笔记

    机器学习中SVD总结 矩阵分解的方法 特征值分解. PCA(Principal Component Analysis)分解,作用:降维.压缩. SVD(Singular Value Decomposi ...

  4. HTML5从入门到精通(千锋教育)免费电子版+PDF下载

    本书是HTML5初学者极好的入门教材之一,内容通俗易懂.由浅入深.循序渐进.本书内容覆盖全面.讲解详细,其中包括标签语义化.标签使用规范.选择器类型.盒模型.标签分类.样式重置.CSS优化.Photo ...

  5. ALV报表——选择屏幕选项卡

    ALV选择屏幕选项卡 运行效果: 代码: *&--------------------------------------------------------------------* *&a ...

  6. PowerBuilder学习笔记之删除和加载PBL文件的方法

    删除PBL目录的方法:直接点删除键删除 加载PBL文件的方法:点Browse按钮选择PBL文件

  7. Kettle部署笔记

    1.启动脚本(启动job) /u02/www/data-integration/kitchen.sh -file:/u02/www/data-integration/job.kjb -logfile= ...

  8. 在论坛中出现的比较难的sql问题:14(存储过程问题 存储过程参数、存储过程内的参数)

    原文:在论坛中出现的比较难的sql问题:14(存储过程问题 存储过程参数.存储过程内的参数) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的 ...

  9. Asp.netCore 是用的Socket 吗?

    Asp.netCore 是用的Socket 的krestrel 用的是Socket! public static IWebHostBuilder CreateDefaultBuilder(string ...

  10. axios拦截登陆过期请求多次

    request.interceptors.response.use( response => { console.log(response.data.code) // console.log(r ...