Skip to content

Instantly share code, notes, and snippets.

@magicleon94
Created December 17, 2019 11:56
Show Gist options
  • Save magicleon94/771609088052a3256d67a6cec84a93a8 to your computer and use it in GitHub Desktop.
Save magicleon94/771609088052a3256d67a6cec84a93a8 to your computer and use it in GitHub Desktop.
Dart streams - Flutter BLoC talk example
<!-- 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>
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!");
}
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