SSE

Getting data from the server, little by little

void establishSSEConnection() {
    http.Client client = http.Client();
    http.Request request = http.Request(
        'GET', Uri.parse('http://127.0.0.1:5000/?prompt=cuentame un chiste'));

    StreamSubscription<http.StreamedResponse> streamSubscription;

    streamSubscription = client.send(request).asStream().listen((response) {
      response.stream.listen((event) {
        print('sse');
        print(String.fromCharCodes(event));
        // Handle the received event
        // The event will be of type http.StreamedResponse
        // You can extract data using event.stream or event.stream.bytesToString()
      }, onError: (error) {
        // Handle any error that occurs during the connection or data retrieval
      }, cancelOnError: true);
    }, onError: (error) {
      // Handle any error that occurs while establishing the connection
    });
  }

Last updated