createResponseTransformer function
Creates a StreamTransformer that intercepts HTTP response data, forwarding chunks to the platform via the method channel while continuing to pass the data downstream.
This allows the native side (e.g., Chrome DevTools) to inspect HTTP response streams in real-time.
Implementation
StreamTransformer<List<int>, List<int>> createResponseTransformer(String id) {
return StreamTransformer.fromHandlers(
handleData: (data, sink) {
// Forward data downstream
sink.add(data);
// Notify native side of new data chunk
MethodChannelController.onDataReceived({"data": data, "id": id});
},
handleError: (error, stacktrace, sink) {
// Forward error downstream
sink.addError(error, stacktrace);
// Notify native side of read failure
MethodChannelController.responseReadFailed([id, error.toString()]);
},
handleDone: (sink) {
// Close downstream sink
sink.close();
// Notify native side that the response has finished reading
MethodChannelController.responseReadFinished(id);
MethodChannelController.onDone(id);
},
);
}