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. Zuul【基础配置】

    概述:zuul底层是基于servlet,是由一系列的filter链构成. 1.路由配置 a.单例serverId映射 zuul: routes: client-a: path: /client/** ...

  2. 【优先队列】Function

    Function 题目描述 wls有n个二次函数Fi(x)=aix2+bix+ci(1≤i≤n).现在他想在且x为正整数的条件下求的最小值.请求出这个最小值. 输入 第一行两个正整数n,m.下面n行, ...

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

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

  4. react实现设置答题器选项个数

    一,设置答题器选项import React, { useState, useEffect } from 'react' import PropTypes from 'prop-types' impor ...

  5. RabbitMQ的应用场景

    进入正题. 一.异步处理 场景:发送手机验证码,邮件 传统古老处理方式如下图 这个流程,全部在主线程完成,注册->入库->发送邮件->发送短信,由于都在主线程,所以要等待每一步完成才 ...

  6. EfCore基本用法

    db first 和 code first的基本使用方法 https://www.cnblogs.com/Starts_2000/p/mysql-efcore20-codefirst-dbfirst- ...

  7. selenium异常

    记一下让我花时间去找解决办法的异常 org.openqa.selenium.ElementNotInteractableException: element not interactable 第一次出 ...

  8. MySql 8.0服务端安装后,用navicat12连接时报2059错误_解决

    先看连接错误 连接失败:2059 - Authentication plugin 'caching_sha2_password' cannot be loaded: .... 解决方法: 进入MySQ ...

  9. LTS秘钥协商算法分析

    1.根据RCF文档说法 在1-RTT中有两种密钥协商算法(1-RTT ECDHE和 1-RTT PSK  )和4中0-RTT密钥协商方式(0-RTT PSK, 0-RTT ECDH ,0-RTT EC ...

  10. CIP 协议安全扫盲