Created
December 17, 2019 11:56
-
-
Save magicleon94/771609088052a3256d67a6cec84a93a8 to your computer and use it in GitHub Desktop.
Dart streams - Flutter BLoC talk example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Copyright 2011 the Dart project authors. All rights reserved. | |
Use of this source code is governed by a BSD-style license | |
that can be found in the LICENSE file. --> | |
<h2>Dart streams!</h2> | |
<div> | |
<canvas id="canvas" width="300" height="300"></canvas> | |
</div> | |
<div> | |
<label>0</label> | |
<input id="slider" type="range" max="1000" value="500" /> | |
<label>1000</label> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "dart:async"; | |
void generate(StreamSink sink) async { | |
print("Someone is listening. Generating numbers!"); | |
for (int i = 0; i <= 10; i++) { | |
await Future.delayed(Duration(seconds: 1)); | |
sink.add(i); | |
} | |
sink.close(); | |
} | |
void main() async { | |
StreamController<int> myStreamController = StreamController<int>.broadcast(); | |
myStreamController.onListen = () => generate(myStreamController.sink); | |
StreamSubscription subscription = | |
myStreamController.stream.listen((value) => print("Value: $value")); | |
await for (int value | |
in myStreamController.stream.map((value) => value * value)) { | |
print("Squared value: $value"); | |
} | |
print("Job completed, clenaing up..."); | |
subscription.cancel(); | |
myStreamController.close(); | |
print("Bye!"); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
h2 { | |
margin-bottom: 0; | |
text-align: center; | |
} | |
div { | |
text-align: center; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment