Search code examples
eventsflutterreactive-programmingrxdart

What type should I use in an Rx stream for notifications purpose?


I build a flutter app using RxDart and I want to create a stream of click events. What type should this stream contain? bool would probably work for me, but I'll always stream trues. Is there any best practice?


Solution

  • You could use a Stream<void>:

    final clickEventStream = PublishSubject<void>();
    

    When you are listening to the stream, ignore the argument using an underscore:

    clickEventStream.listen((_) { ... })
    

    To push new events to the stream, just add null (it's a valid value for void):

    clickEventStream.add(null);
    

    Also see https://medium.com/dartlang/dart-2-legacy-of-the-void-e7afb5f44df0