title: How to create a StreamTransformer in Dart?
body: <p>Trying to build a custom StreamTransformer class, however a lot of the examples out there seem to be out of date, and the one found in the documentation isn't (what some typed languages might consider anyway) as a class (found here: <a href="https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart:async.StreamTransformer" rel="noreferrer">https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart:async.StreamTransformer</a>). This doesn't seem like a very Dart-like way of approaching it and rather more of a Javascript-like way (which I'm using Dart to avoid). </p>
  
  <p>Many online sources say this is how you create a StreamTransformer, however there errors when extending it.</p>
  
  <pre><code>class exampleStreamTransformer extends StreamTransformer
  {
    //... (This won't work)
  }
  </code></pre>
  
  <p>'Implements' seems to be the way to go, along with implementing the bind function needed:</p>
  
  <pre><code>class exampleStreamTransformer implements StreamTransformer
  {
    Stream bind(Stream stream)
    {
      //... (Go on to return new stream, etc)
    }
  }
  </code></pre>
  
  <p>I can't seem to find any examples of this way, but have thrown something together myself (which is accepted in my IDE, but isn't accepted at runtime, I get a null object error when it tries to use pause getter):</p>
  
  <pre><code>class exampleStreamTransformer implements StreamTransformer
  {
    StreamController&lt;String&gt; _controller;
    StreamSubscription&lt;String&gt; _subscription;
  
    Stream bind(Stream stream)
    {
      _controller = new StreamController&lt;String&gt;(
          onListen: ()
          {
            _subscription = stream.listen((data)
            {
              // Transform the data.
              _controller.add(data);
            },
            onError: _controller.addError,
            onDone: _controller.close,
            cancelOnError: true); // Unsure how I'd pass this in?????
          },
          onPause: _subscription.pause,
          onResume: _subscription.resume,
          onCancel: _subscription.cancel,
          sync: true
      );
  
      return _controller.stream;
    }
  }
  </code></pre>
  
  <p>Would like to achieve it this way, as in the 'typed' way of producing the class, any help is much appreciated, thank you.</p>