Why http.get is throwing error in Flutter

Issue

The error message that is showing in Dev toolsI 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 : LINK
Comment if the solution does not work.

Answered By – Nagulan S

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.