1

I am trying to do a super simple http.get in dart with Flutter but I am not getting a result, it gets blocked.

Any idea why this happens?

Edit1: after a minute of waiting, it got to the catch the following exception:

SocketException: Failed host lookup: 'worldtimeapi.org' (OS Error: No address associated with hostname, errno = 7)

My code:

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

void getTime() async {
    try{
      var url = Uri.parse('http://worldtimeapi.org/api/timezone/Europe/London');
      // var url = Uri.parse('https://catfact.ninja/fact');
      final response = await http.get(url);   //await causes a block forever. nothing happens afterwards
      // Map data = jsonDecode(response.body);
      print(response);
    }catch(e){
      print(e);
    }
  }

Moaz El-sawaf
  • 2,306
  • 1
  • 16
  • 33
eyal gromzin
  • 155
  • 2
  • 11
  • What happens if you `print(await http.get(url))`? – thinkgruen May 28 '22 at 08:40
  • 1
    Try changing the `http` to `https`. `https://worldtimeapi.org/api/timezone/Europe/London` – Harish May 28 '22 at 08:45
  • Can you please share the output of the print inside the `catch` block? – Moaz El-sawaf May 28 '22 at 08:51
  • You must get *something*? What is `response` when you print it? Alternatively, what is `e` when it gets printed? – nvoigt May 28 '22 at 09:46
  • after a minute it got to the catch : SocketException: Failed host lookup: 'worldtimeapi.org' (OS Error: No address associated with hostname, errno = 7) ill check it .!!! – eyal gromzin May 28 '22 at 09:49
  • ok, im not sure what i did, probably its the solution below . but now it works!!! – eyal gromzin May 28 '22 at 10:02
  • @eyalgromzin Can you please check the new answer and accept it? it is the same as the previous accepted answer but referring to the original answer by me because the first one got deleted due to duplication. – Moaz El-sawaf May 29 '22 at 17:09

1 Answers1

1

The problem is because of not allowing the Clear Text Traffic, which in this case is the HTTP, check this answer to get the steps for allowing it.

Moaz El-sawaf
  • 2,306
  • 1
  • 16
  • 33