Mobile Development Flutter Subjective
Oct 03, 2025

How to make HTTP requests in Flutter?

Detailed Explanation
Flutter HTTP requests:\n\n**Using http package:**\n\nimport 'package:http/http.dart' as http;\n\n// GET request\nFuture fetchData() async {\n final response = await http.get(\n Uri.parse('https://api.example.com/data')\n );\n \n if (response.statusCode == 200) {\n return response.body;\n } else {\n throw Exception('Failed to load data');\n }\n}\n\n// POST request\nFuture postData(Map data) async {\n final response = await http.post(\n Uri.parse('https://api.example.com/data'),\n headers: {'Content-Type': 'application/json'},\n body: jsonEncode(data)\n );\n return response.body;\n}\n\n\n**Using FutureBuilder:**\n\nFutureBuilder(\n future: fetchData(),\n builder: (context, snapshot) {\n if (snapshot.hasData) {\n return Text(snapshot.data!);\n } else if (snapshot.hasError) {\n return Text('Error: ${snapshot.error}');\n }\n return CircularProgressIndicator();\n }\n)\n
Discussion (0)

No comments yet. Be the first to share your thoughts!

Share Your Thoughts
Feedback