Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get last value in a Stream<String> using rxdart?

I have a stream that holds string values and i want to get the last value in that string. what's the best way to do it?

  Stream<String> get searchTextStream {
    return _searchController.stream.transform(
      StreamTransformer<String, String>.fromHandlers(
        handleData: (value, sink) {
          if (value.isEmpty || value.length < 4) {
            sink.addError('please enter some text..');
          } else {
            sink.add(value);
          }
        },
      ),
    );
  }
like image 470
Salma Avatar asked Feb 02 '19 10:02

Salma


1 Answers

Get the rxDart package from here https://pub.dartlang.org/packages/rxdart.

import 'package:rxdart/rxdart.dart'; // 1. Import rxDart package.
final _searchController = BehaviorSubject<String>();  // 2. Initiate _searchController as BehaviorSubject in stead of StreamController.

String _lastValue = _searchController.value; // 3. Get the last value in the Stream.

BehaviorSubject is an Object rxDart package. It works like a StreamController, by default the broadcast() is on and BehaviorSubject has more function than StreamController.

like image 83
flamel2p Avatar answered Oct 24 '22 02:10

flamel2p