Flutter web app freezes when http get request is made

Viewed 518

I would like to fetch information from an API using an HTTP GET request to a public endpoint which returns XML, in Flutter (web).

However, when I run the code in debug mode, and I reach the screen where the API HTTP request is made, app freezes.

I wrote the following code:

import 'dart:async';

import 'package:http/http.dart' as http;
import 'package:xml/xml.dart' as xml;

class SearchApi {

Future fetchPhotos(String query) async {
  final http.Response response = await http.get('https://www.gahetna.nl/beeldbank-api/opensearch/?searchTerms=auto&startIndex=1&count=10', headers: {"Accept": "application/xml"});

  print(response.body);

  if (response.statusCode == 200) {
    // If the server did return a 200 OK response,
    // then parse the XML.
    print(xml.parse(response.body));

    return xml.parse(response.body);
  } else {
    // If the server did not return a 200 OK response,
    // then throw an exception.
    throw Exception('Failed to load photos');
  }
}

The code works when I change the http.get url to for instance 'https://jsonplaceholder.typicode.com/todos/1'. The http get request works to the 'https://www.gahetna.nl/beeldbank-api/opensearch/' endpoint, when I use curl in command line.

There are no error messages shown in Visual Studio Code IDE when the screen freezes.

0 Answers
Related