I try to receive chunk of data that send from api, and it work if I run command flutter test test/widget_test.dart it enter in loop many time but if I debug my flutter app it not get stream it get whole response and it enter loop just one time
Future<void> response(String body) async {
var client = http.Client();
Map<String, String> headers = {
"Accept": "application/json",
'Content-Type': 'application/json',
'Connection': 'Keep-Alive',
'Authorization': "Bearer 'mocktoken'"
};
try {
var request = http.Request("POST",
Uri.parse("https://localhost:5001/api/ChatBot/chatbot_requestquestion"));
request.body = body;
request.headers.addAll(headers);
var response = await client.send(request);
await for(var value in response.stream.transform(utf8.decoder)){
print(value);
}
// Process the stream of response data in chunks
// print(response.headers);
// response.stream.transform(utf8.decoder).listen((chunk) {
// print(chunk); // Adding each chunk to the stream
// }, onDone: () {
// client.close();
// }, onError: (error) {
// print('Error: $error');
// client.close();
// }, cancelOnError: true);
} catch (e) {
print(e);
throw e;
}
}
void main() async {
await response();
}
I want it work in my app
New contributor
A iZU is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.