ZetCode

Dart WritePipe

last modified April 4, 2025

The WritePipe class in Dart provides an efficient way to write data to streams. It's useful for handling I/O operations, network communication, and data processing pipelines.

WritePipe manages data writing operations asynchronously, ensuring efficient memory usage and proper error handling. It's part of Dart's core libraries.

Basic Definition

WritePipe is a utility for writing data to output streams. It handles backpressure and provides methods for writing different data types.

Key features include chunked writing, error handling, and stream management. It's particularly useful for writing large amounts of data efficiently.

Basic WritePipe Usage

This example shows basic data writing using WritePipe with a file.

main.dart
import 'dart:io';

void main() async {
  final file = File('output.txt');
  final sink = file.openWrite();
  final pipe = WritePipe(sink);
  
  await pipe.write('Hello, World!');
  await pipe.close();
  
  print('Data written to file');
}

We create a WritePipe connected to a file sink, write data, and close it. The pipe handles all the asynchronous operations and resource management.

$ dart main.dart
Data written to file

Writing Multiple Chunks

This example demonstrates writing data in multiple chunks using WritePipe.

main.dart
import 'dart:io';

void main() async {
  final file = File('data.log');
  final sink = file.openWrite();
  final pipe = WritePipe(sink);
  
  for (var i = 0; i < 5; i++) {
    await pipe.write('Chunk $i\n');
    print('Wrote chunk $i');
  }
  
  await pipe.close();
  print('All chunks written');
}

We write data in multiple chunks with proper sequencing. WritePipe ensures each write operation completes before starting the next one.

$ dart main.dart
Wrote chunk 0
Wrote chunk 1
Wrote chunk 2
Wrote chunk 3
Wrote chunk 4
All chunks written

Error Handling

This example shows proper error handling with WritePipe operations.

main.dart
import 'dart:io';

void main() async {
  try {
    final file = File('/invalid/path/data.txt');
    final sink = file.openWrite();
    final pipe = WritePipe(sink);
    
    await pipe.write('Test data');
    await pipe.close();
  } catch (e) {
    print('Error occurred: $e');
  }
}

WritePipe operations can throw exceptions which should be caught and handled. This example demonstrates proper error handling for file operations.

$ dart main.dart
Error occurred: FileSystemException: Cannot open file, path = '/invalid/path/data.txt' (OS Error: No such file or directory, errno = 2)

Writing Binary Data

This example demonstrates writing binary data through a WritePipe.

main.dart
import 'dart:io';
import 'dart:typed_data';

void main() async {
  final file = File('binary.dat');
  final sink = file.openWrite();
  final pipe = WritePipe(sink);
  
  final data = Uint8List.fromList([0, 1, 2, 3, 4, 5]);
  await pipe.write(data);
  
  await pipe.close();
  print('Binary data written');
}

We create a Uint8List with binary data and write it through the pipe. WritePipe handles the binary data conversion and streaming automatically.

$ dart main.dart
Binary data written

Network Communication

This example shows using WritePipe for network socket communication.

main.dart
import 'dart:io';

void main() async {
  final socket = await Socket.connect('example.com', 80);
  final pipe = WritePipe(socket);
  
  await pipe.write('GET / HTTP/1.1\r\n');
  await pipe.write('Host: example.com\r\n');
  await pipe.write('Connection: close\r\n\r\n');
  
  await pipe.close();
  print('HTTP request sent');
  
  socket.listen((data) {
    print(String.fromCharCodes(data).trim());
  });
}

We connect to a web server and send an HTTP request using WritePipe. The pipe ensures proper sequencing of the request lines and headers.

$ dart main.dart
HTTP request sent
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
...

Best Practices

Source

Dart WritePipe Documentation

This tutorial covered Dart's WritePipe class with practical examples showing basic usage, network communication, binary data handling, and error management.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Dart tutorials.