Search code examples
flutterhttpget

Why http.get is throwing error in Flutter


The error message that is showing in Dev tools: error

I am trying to execute a PHP file in Flutter.

The code that I have is:

class _MyAppState extends State<MyApp> {
  @override
  var url = 'http://localhost/myDashFolder/getdata.php';

  Future<List> getData() async {
    final response = await http.get(Uri.parse(url));
    if (response.statusCode == 200) {
      return json.decode(response.body);
    }
    else {
      throw Exception("Response content length is ${response.statusCode}, failed to get any details.");
    }
  }

In Dev tools, the get request is showing an error. However, when I execute the same manually, it works.

What am I doing wrong?


Solution

  • url = http://10.0.2.2/myDashFolder/getdata.php

    Try using the above URL in your emulator.

    Source : https://inspirnathan.com/posts/34-access-localhost-inside-android-emulator/