Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Future & Stream in Dart
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Tomochika Hara
July 27, 2013
Technology
510
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Future & Stream in Dart
2013 Future & Stream in Dart
Tomochika Hara
July 27, 2013
More Decks by Tomochika Hara
See All by Tomochika Hara
Swiftでつくるファミコンエミュレータのススメ
thara
3
2.5k
モバイルゲーム開発と Google App Engine
thara
0
410
GCPとAWSの比較
thara
0
140
Dart VM と Optional Typing
thara
0
430
Dart in なごやまつり
thara
0
63
Dartの基本
thara
0
87
Dart言語仕様 Pick-up
thara
0
660
Other Decks in Technology
See All in Technology
20260906 「AWS運用入門」著者が教える、運用業務への生成AI活用入門
masaruogura
0
200
分割40%キーボードにスムーズに入門するには
hoto17296
1
220
Webとヘルスデータ
yukukotani
0
160
Amazon S3 Tablesに全部任せてみた結果——コンパクション/スナップショット管理は本当に手放せるか
shigeruoda
0
300
JAWS-UG初心者支部#88わいわい初心塾(夏休みの宿題やったかGit編)
otsuki
0
110
Jetpack Compose で挑む新聞紙面UI ─ 複合ジェスチャー・ポリゴン記事領域・適応的ページ構成という3つの壁/droidkaigi2026
nikkei_engineer_recruiting
0
160
プロダクトエンジニアに必要な「いい感じ」に作る能力 〜たくさん作れる時代に、どこまで作るかの決め方〜
jnishime_dresscode
2
1.1k
KPIだけでは評価できないプロダクトが考えるべき Evalsという第二の評価系 / Beyond KPIs: Evals as a Second Evaluation Framework for Products #PdEConf
aki_iinuma
3
3.3k
When Token Pruning is Worse than Random: Understanding Visual Token Information in VLLMs
sansantech
PRO
0
240
全社に広がるMCPサーバーを、 どう安全に管理するか MCPass開発の舞台裏
mtpooh
3
270
[RSJ26] Building a VLA Model Based on Self-Distilled Classification
keio_smilab
PRO
0
180
『止めない』を設計する — 制約の中で、事業の根幹を支える判断
hiroyaterui
0
120
Featured
See All Featured
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
360
A designer walks into a library…
pauljervisheath
211
24k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2.1k
The Cult of Friendly URLs
andyhume
79
7k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
1k
Embracing the Ebb and Flow
colly
88
5.2k
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
180
Building Applications with DynamoDB
mza
96
7.2k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
490
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.8k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
430
Introduction to Domain-Driven Design and Collaborative software design
baasie
1
970
Transcript
Future & Stream in Dart tomochikahara @zetta1985
Agenda • Future API • Stream API
Future API for Ansynchronous Programming
What is a Future? • 「先物」 ◦ 別名Promise。 ◦ 処理結果を必要となる時点まで保持する
◦ 並列処理のデザインパターン ▪ Dartでは非同期なだけで並列処理は必須でない • Futureの値を決めるCompleter ◦ Futureはあくまでも値を保持するのみ ◦ 計算結果をFutureに与えるのがCompleter • dart:io, indexed_dbなどで利用
Future & Completer Completer Future Future Caller Callee Future<String> echoToMountain(String
yo_ho); call echoToMountain create get Future return Future then((reply) => catchMyEar(reply)) complete(reply) Execution
Future & Completer import "dart:async"; Future<String> echoToMountain(String yo_ho) { var
completer = new Completer<String>(); new Timer(new Duration(milliseconds: 5000), () { completer.complete(yo_ho); }); return completer.future; } void main() { print("before echoToMountain"); var future = echoToMountain("YooooHooooo"); future.then(print); print("after echoToMountain"); }
Future Example new File("./foo.txt").fullPath() .then((fullpath) { print("foo.txt's fullpath is $fullpath");
}) .catchError((e) { print("foo.txt is not exists."); }); File IO (Async try-catch) HTTP Request (Async Process Chain) var uri = Uri.parse("http://tomochikahara.com"); new HttpClient().getUrl(uri) .then((HttpClientRequest req){ // prepare request req.headers.add("contentType", "text/html"); return req.close(); }).then((HttpClientResponse res) { res.transform(new StringDecoder()).forEach(print); });
Stream API for sequence of any data
What is a Stream? • イベントの「流れ」 ◦ 単なる遅延リスト。 ◦ 何回も使えるFuture。
◦ 値が発すること=イベント、と表現 ◦ Producer - Consumer Pattern ▪ Dartでは非同期なだけで並列処理は必須でない • DOM EventやByte IO、標準入力などで利用
Stream What is a Stream? Controller (Sink) Subscription D C
B' A' ・・・ Transformer F E add listen
Stream var controller = new StreamController<String>(); controller.add("A"); var stream =
controller.stream; stream.listen((str) => print(str), onError : (e) => print("Error"), onDone : () => print("Done")); controller.add("B"); controller.add("C"); controller.addError(new StateError("Illegal State")); controller.add("D"); controller.close();
Stream Subscription var controller = new StreamController<String>(); controller.add("A"); var stream
= controller.stream; var subscription = stream.listen((str) => print(str), onDone : () => print("Done")); subscription.onError((e) { subscription.cancel(); }); controller.add("B"); controller.add("C"); controller.addError(new StateError("Illegal State")); controller.add("D"); controller.close();
Stream Transformer var controller = new StreamController<int>(); var stream =
controller.stream; var transformer = new StreamTransformer<int, String>( handleData : (int value, EventSink<String> sink) { sink.add("$value:(1)"); sink.add("$value:(2)"); }, handleDone : (EventSink<String> sink) { sink.add("Done!"); } ); stream.transform(transformer).forEach(print); for (var i = 0; i < 10; i++) { controller.add(i); } controller.close();
Stream Example query("input#hello").onClick.listen((MouseEvent e) { window.alert("hello, world."); }); DOM Event
File Access StreamSubscription<List<int>> subscription; subscription = new File('bar.txt').openRead().listen((bytes) { for (var b in bytes) { print(new String.fromCharCode(b)); if (b == '#'.codeUnitAt(0)) { subscription.cancel(); return; } } });
Stream Example 標準入力 HTTP Server HttpServer.bind("127.0.0.1", 8080).then((HttpServer server) { server.listen((HttpRequest
req) { req.response.write("This message from my server."); req.response.close(); }); }); print("Please Input A."); var stream = stdin .transform(new StringDecoder()) .transform(new LineTransformer()); stream.listen((str){ print("Your input's length : ${str.length}"); print(str == "A" ? "Just A." : "Not A."); });