readAsString method

Future<String> readAsString([
  1. Encoding encoding = Closeable.DEFAULT_ENCODING
])

Reads all remaining bytes from this input stream and decodes them into a String.

This method reads the entire stream from the current position until the end and then converts the bytes to a string using the specified encoding.

The default encoding is Closeable.DEFAULT_ENCODING.

Parameters

Returns

A String representing the decoded contents of the input stream.

Example

final input = FileInputStream('message.txt');
try {
  final text = await input.readAsString(encoding: Closeable.DEFAULT_ENCODING);
  print('File contents: $text');
} finally {
  await input.close();
}

Throws

Implementation

Future<String> readAsString([Encoding encoding = Closeable.DEFAULT_ENCODING]) async {
  checkClosed();

  try {
    final bytes = await readAll();
    return encoding.decode(bytes);
  } on FormatException catch (e) {
    throw IOException('Failed to decode stream: ${e.message}');
  }
}