Home > database >  Why http.get is throwing error in Flutter
Why http.get is throwing error in Flutter

Time:03-23

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?

CodePudding user response:

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.

  • Related