lang/dart

Dart onClick, timeout, take, where, onDone

C/H 2020. 9. 11. 18:09

Dart Sink

import 'dart:html';

void main() {
  final btn = querySelector('button');

  btn.onClick
    .timeout(
      new Duration(seconds: 1),    // 1초 후
      onTimeout: (sink) => sink.addError('You Lost')    // sink에 에러 추가
    )
    .listen(
      (event) {},
      onError: (err) => print(err)
    );

}
<button>Click</button>

import 'dart:html';

void main() {
  final ButtonElement btn = querySelector('button');
  final InputElement input = querySelector('input');

  btn.onClick
      .take(4)
      .where((event)=>input.value == 'banana')
      .listen(
        (event) => print('You got it'), 
        onError: (err) => print(err),
        onDone: () => print('Nope, bad guesses.')
       );
}
<input placeholder="Input text" />
<button>Click</button>

import 'dart:html';
import 'dart:async';

void main() {
  final InputElement input = querySelector('input');
  final DivElement div = querySelector('div');

  final validator = new StreamTransformer.fromHandlers(
    handleData: (inputValue, sink) {
      if( inputValue.contains('@') ) {
        sink.add(inputValue);
      } else {
        sink.addError('Enter a valid email');
      }
    }
  );

  input.onInput
    .map((dynamic event) => event.target.value)
    .transform(validator)
    .listen(
      (inputValue) {
        div.innerHtml='';
        print('valid Email: $inputValue');
      },
      onError: (err) => div.innerHtml=err
    );

}
<input placeholder="Input text" />
<div style="color: red"></div>

반응형

'lang > 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 Async, Stream, StreamController, StreamTransformer  (0) 2020.09.11