skip to Main Content

i am trying to do an http request with flutter, with the konamicash.com/connexion_bdd_flutter, i have no answer even no error, but with the same code with the url of

https://jsonplaceholder.typicode.com/todos/1

echo json_encode(["message"=>"requete ok"]);

?>

 fetchData() async { var url = Uri.parse('https://konamicash.com/connexion_bdd_flutter'); var response = await http.get(url);

}
if (response.statusCode == 200) {
  print('sa fonctionne: ${response.body}');
} else {
  print('error');
}

}

2

Answers


  1. Import http package as

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

    Try below code with try/catch block for errors and data response.

    Future<void> fetchData() async {
      try {
        var url = Uri.parse('https://konamicash.com/connexion_bdd_flutter');
        var response = await http.get(url);
    
        if (response.statusCode == 200) {
          print('sa fonctionne: ${response.body}');
        } else {
          print('Error: ${response.statusCode}');
        }
      } catch (e) {
        print('An error occurred: $e');
      }
    }
    
    Login or Signup to reply.
  2. I’ve tested your function, and it’s working properly. However, in your function, there were syntax mistakes, and the condition was placed outside the function.

     fetchData() async {
        var url = Uri.parse('https://konamicash.com/connexion_bdd_flutter');
        var response = await http.get(url);
          if (response.statusCode == 200) {
            print('sa fonctionne: ${response.body}');
          } else {
            print('error');
          }
      }
    

    I’ve made some corrections to your code and added certain changes. However, I recommend following proper syntax when working with Future. Below, I’ve redefined your function correctly.

    I’ve defined the return type of the function and added a try-catch block for error handling.

       Future<void> fetchData() async {
        var url = Uri.parse('https://konamicash.com/connexion_bdd_flutter');
        var response = await http.get(url);
    
        try {
          if (response.statusCode == 200) {
            print('sa fonctionne: ${response.body}');
          } else {
            print('error');
          }
        } catch (e, s) {
          print(e);
        }
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search