Seth Ladd
Seth Ladd

Reputation: 120549

How can I create an InputStream from a String in Dart?

StringInputStream looked like the right name, but rather than make an InputStream out of a String, it wraps an existing InputStream to read lines out of it.

I tried to find a base InputStream class to extend and ended up finding stream_utils.dart which I had to copy into my project in order to use.

Will there be ways to create InputStreams and OutputStreams for Strings, byte arrays and such?

Upvotes: 4

Views: 1634

Answers (1)

Seth Ladd
Seth Ladd

Reputation: 120549

Here's how you can do it:

import 'dart:io';

main() {
  var input = "hello from dart";
  var inputStream = new ListInputStream();
  inputStream.onData = () {
    print(new String.fromCharCodes(inputStream.read()));
  };

  inputStream.write(input.charCodes());
}

Upvotes: 5

Related Questions