better_networking: streaming implementation (streamHttpRequest)

This commit is contained in:
Manas Hejmadi
2025-06-16 17:14:07 +05:30
parent ee75dd6fed
commit 80d2ca69d7
2 changed files with 153 additions and 11 deletions

View File

@@ -50,3 +50,20 @@ Future<http.Response> convertStreamedResponse(
return response;
}
Stream<String?> streamTextResponse(
http.StreamedResponse streamedResponse,
) async* {
try {
if (streamedResponse.statusCode != 200) {
final errorText = await streamedResponse.stream.bytesToString();
throw Exception('${streamedResponse.statusCode}\n$errorText');
}
final utf8Stream = streamedResponse.stream.transform(utf8.decoder);
await for (final chunk in utf8Stream) {
yield chunk;
}
} catch (e) {
rethrow;
}
}