当前位置:网站首页>Dart series: using generators in dart
Dart series: using generators in dart
2022-06-24 00:10:00 【Procedural stuff】
brief introduction
ES6 While introducing asynchronous programming , It has also been introduced. Generators, adopt yield Keywords to generate the corresponding data . alike dart Also have yield Keywords and generator concepts .
When is the generator ? The so-called generator is a device that can continuously generate some data , It's also called generator.
Two return types generator
It depends on whether it is generated synchronously or asynchronously ,dart The results returned are also different .
In case of synchronous return , So the return is a Iterable object .
If it's an asynchronous return , So the return is a Stream object .
synchronous generator Use sync* The key words are as follows :
Iterable<int> naturalsTo(int n) sync* {
int k = 0;
while (k < n) yield k++;
}Asynchronous generator It uses async* The key words are as follows :
Stream<int> asynchronousNaturalsTo(int n) async* {
int k = 0;
while (k < n) yield k++;
}Generate keywords using yield.
If yield Followed by itself is a generator, Then you need to use yield*.
Iterable<int> naturalsDownFrom(int n) sync* {
if (n > 0) {
yield n;
yield* naturalsDownFrom(n - 1);
}
}Stream The operation of
stream Represents a stream , After getting this stream , We need to extract the corresponding data from the stream .
from Stream There are two ways to get data from , The first is to use Stream Of itself API To get Stream Data in .
The simplest is to call stream Of listen Method :
StreamSubscription<T> listen(void onData(T event)?,
{Function? onError, void onDone()?, bool? cancelOnError});listen Processing methods that can receive data , The specific use is as follows :
final startingDir = Directory(searchPath);
startingDir.list().listen((entity) {
if (entity is File) {
searchFile(entity, searchTerms);
}
});The default method is onData Method .
The other is what we're going to talk about today await for.
await for The grammar is as follows :
await for (varOrType identifier in expression) {
// Executes each time the stream emits a value.
}Note that the above expression Must be a Stream object . also await for Must be used in async in , as follows :
Future<void> main() async {
// ...
await for (final request in requestServer) {
handleRequest(request);
}
// ...
}If you want to interrupt stream Listening in , You can use break perhaps return.
summary
That's all dart The use of the generator in .
This article has been included in http://www.flydean.com/13-dart-generators/ The most popular interpretation , The deepest dry goods , The most concise tutorial , There are so many tricks you don't know about waiting for you to discover !
边栏推荐
- Startup process analysis of APP performance optimization
- 【数字信号】基于matlab模拟窗函数频谱细化【含Matlab源码 1906期】
- 社招面试必不可少——《1000 道互联网大厂 Android工程师面试题》
- 物联网卡设备接入EasyCVR,如何查看拉流IP以及拉流时间?
- Vulnerability recurrence - redis vulnerability summary
- Shutter time selector
- [leetcode notes] no118 Yanghui triangle
- Android - JNI 开发你所需要知道的基础,Android工程师面试题
- 重载(Overload)和重写(Override)的区别?
- Activity 的 36 大难点,你会几个?,安卓面试2020
猜你喜欢

fatal: The upstream branch of your current branch does not match the name of your current branch.

Confused test / development programmers, different people have different stories and different puzzles

人工智能技术岗位面试要注意什么?

Android AIDL:跨进程调用Service (AIDL Service),kotlininvoke函数

测试 - 用例篇 - 细节狂魔

数据库中索引原理及填充因子

Dot and cross product

迷茫的测试/开发程序员,不同人有着不同的故事、有着不同的迷茫......

依赖倒置原则
![[interview experience package] summary of experience of being hanged during interview (I)](/img/ab/ccee8e624248840e712c0b4ca417dd.png)
[interview experience package] summary of experience of being hanged during interview (I)
随机推荐
Android - basics you need to know about JNI development, interview questions for Android engineers
逆向工具IDA、GDB使用
How to get started with machine learning?
Docker redis cluster configuration
Test - use case - detail frenzy
Interpreting the "four thoughts" of Wal Mart China President on the transformation and upgrading of physical retail
Keywords such as extern and struct
NLP工程师是干什么的?工作内容是什么?
[image detection saliency map] calculation of fish eye saliency map based on MATLAB distortion prompt [including Matlab source code 1903]
Do280openshift access control -- manage projects and accounts
迷茫的测试/开发程序员,不同人有着不同的故事、有着不同的迷茫......
国内首款开源MySQL HTAP数据库即将发布,三大看点提前告知 石原子科技重磅推出
Summary of common register bit operation modes in MCU
extern、struct等关键字
微信小程序中three.js的canvas非全屏情况下射线检测不准确问题解决方案
Expander+listbox of WPF effect
Android 7,2021最新Android面试笔试题目分享
Cloud native architecture (05) - Application Architecture Evolution
Revit API:明细表 ViewSchedule
【第25天】给定一个长度为 n 的数组,统计每个数出现的次数 | 计数哈希