textStream method

BehaviorSubject<String> textStream()

A BehaviorSubject that will update text when a new event is emitted, also emits a new event when text changes

Implementation

BehaviorSubject<String> textStream() {
  final stream = BehaviorSubject<String>.seeded(this.text);
  bool isLock = false;

  // Set a new text to controller
  // when stream is emitted
  stream.listen((text) {
    if (isLock) return;
    isLock = true;
    this.text = text;
    isLock = false;
  });

  // Set value back to stream
  // when the text got changed in controller
  this.addListener(() async {
    if (isLock) return;
    isLock = true;
    stream.value = this.text;
    await Future.delayed(Duration(microseconds: 1));
    isLock = false;
  });
  return stream;
}