read<T> method

Future<T> read<T>(
  1. String path,
  2. FutureOr<T> block(
    1. BufferedSource source
    ), [
  3. int? start,
  4. int? end,
])

Creates a source to read path, executes block to read it, and then closes the source. This is a compact way to read the contents of a file.

Implementation

Future<T> read<T>(
    String path, FutureOr<T> Function(BufferedSource source) block,
    [int? start, int? end]) {
  return source(path, start, end).buffered().then((e) async {
    try {
      return await block(e);
    } finally {
      try {
        await e.close();
      } catch (_) {}
    }
  });
}