본문 바로가기

Language/Dart

Dart Async, Stream, StreamController, StreamTransformer

StreamController, StreamTransformer.fromHandlers

import 'dart:async';

class Cake {}
class Order {
  String type;
  Order(this.type);
}

void main() {
  final controller = new StreamController();

  final order = new Order('banana');
  final baker = new StreamTransformer.fromHandlers(handleData: (cakeType, sink){
    if( cakeType == 'chocolate') {
      sink.add(new Cake());
    } else {
      sink.addError('I cant bake that type!!!');
    }
  });

  controller.sink.add(order);
  controller.sink.add(new Order('apple'));
  controller.sink.add(new Order('chocolate'));

  controller.stream
    .map((order)=>order.type)
    .transform(baker)
    .listen(
      (cake) => print('Heres your cake $cake'),
      onError: (err) => print(err),
    );
}

'Language > Dart' 카테고리의 다른 글

Flutter BLoC, Scoped Instances  (0) 2020.09.16
Flutter BLoC, Single Global Instance  (0) 2020.09.15
Flutter BLoC  (0) 2020.09.14
Dart BLoC Pattern  (0) 2020.09.11
Dart onClick, timeout, take, where, onDone  (0) 2020.09.11
Dart Async, Stream, StreamController, StreamTransformer  (0) 2020.09.11