listen method

StreamSubscription listen(
  1. void handler(
    1. Map<String, dynamic>
    ), {
  2. bool filter(
    1. Map<String, dynamic>
    )?,
})

Listen in on the stream, executing the handler for each message. Optionally filtering the messages that the handler is executed on.

Implementation

StreamSubscription<dynamic> listen(
  void Function(Map<String, dynamic>) handler, {
  bool Function(Map<String, dynamic>)? filter,
}) {
  return stream.listen((msg) {
    Map<String, dynamic> json =
        jsonDecode(msg as String) as Map<String, dynamic>;

    if (filter != null) {
      if (filter(json)) {
        handler(json);
      }
    } else {
      handler(json);
    }
  });
}