当前位置:网站首页>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 !
边栏推荐
- Expander+listbox of WPF effect
- Jimureport building block report - table linkage chart topic
- 微信小程序中three.js的canvas非全屏情况下射线检测不准确问题解决方案
- EasyCVR程序以服务启动异常,进程启动却正常,是什么原因?
- JS language precision problem
- Docker Deployment redis
- Notepad++ practical function sharing (common methods for replacing the end and beginning of regular lines, text comparison function, etc.)
- Salesforce Future method in salesforce – @future
- Restore IP address [standard backtracking + standard pruning]
- 跟着CTF-wiki学pwn——ret2text
猜你喜欢

Android 3年外包工面试笔记,有机会还是要去大厂学习提升,作为一个Android程序员

2. camera calibration

docker 部署redis

Save: software analysis, verification and test platform

Cloud native architecture (05) - Application Architecture Evolution

小猫爪:PMSM之FOC控制15-MRAS法

【面试经验包】面试被吊打经验总结(一)

Another short video app with high imitation and eye opening

Synthetic big watermelon games wechat applet source code / wechat game applet source code

WPF效果之Expander+ListBox
随机推荐
Generate all possible binary search trees
Accompanist组件库中文指南 - Glide篇,劲爆
Comment utiliser l'entrepôt de données pour créer une table de synchronisation
Android App bundle exploration, client development interview questions
超标量处理器设计 姚永斌 第3章 虚拟存储器 --3.1~3.2 小节摘录
Drag and drop report design - new features of jimureport 1.4.0
Chaos engineering, learn about it
Notepad++ practical function sharing (common methods for replacing the end and beginning of regular lines, text comparison function, etc.)
JS language precision problem
Go language core 36 lectures (go language practice and application 11) -- learning notes
物联网卡设备接入EasyCVR,如何查看拉流IP以及拉流时间?
国内首款开源MySQL HTAP数据库即将发布,三大看点提前告知 石原子科技重磅推出
Restore IP address [standard backtracking + standard pruning]
数据库中索引原理及填充因子
Tiktok practice ~ password retrieval
[technical grass planting] the tail of the "double 11" event. Let's talk about how much discount the message push service package is!
Windows10 security mode entry cycle blue screen repair
Cloud native architecture (05) - Application Architecture Evolution
医疗是什么?AI医疗概念解析AI
社招面试必不可少——《1000 道互联网大厂 Android工程师面试题》