Dart HTTP client keeps loading nothing

83 views Asked by At

I hope you can help me. I am not completely new to dart since I already set up the major part of a flutter app. But I need a backend server for my app and I thought a dart server would be convenient because I like the dart language. So I set up a normal online server running Ubuntu and started to develop with VS Code SSH Remote. I need my server to refresh and restructure its stored data so that my app can fetch the data easily. Yesterday I tried downloading the necessary .csv file and everything was working fine. Today, the HTTPClient keeps on loading.

My Code:

refreshData() {
  refresh() async {
    print('refreshing data...');
    var client = await Client()
        .send(Request('GET', Uri.parse(articlesUrl)))
        .timeout(Duration(seconds: 45));
    int total = client.contentLength ?? 0;
    int received = 0;
    client.stream.listen((value) {
      bytes.addAll(value);
      received += value.length;
      print('Received: ' + received.toString() + 'of: ' + total.toString());
    }).onDone(() async {
      await File('assets/data/articles.csv').writeAsBytes(bytes);
      print('file written.');
    });
  }

  refresh();
  const timer = Duration(minutes: 30);
  Timer.periodic(timer, (Timer t) async {
    refresh();
  });
}

The code executes until the print('refreshing data...') normally and then I am completely clueless what happens. Even the timeout function doesnt work, neither does the listen function. But I don't get any Exceptions or Errors thrown. I also tried the Dio package as HTTP Client but it stays the same. The file on the other server is accessible normally, as it was before and curl reports no problem with the server I get the file from. The port is free and the HTTPServer reports no problem.

Did somebody have the same problem or do you know how to debug correctly so I can see what HTTPClient is doing?

1

There are 1 answers

0
Kevin Moore On

A few things

  1. You don't close the client. And you create one every call to refresh.

I'd create one client.

  1. You don't wait for the stream to finish. I'd await client.stream.forEach instead of doing listen.

These things might help you debug