flutter http request
To make an HTTP request in Flutter, you can use the http
package. Here's an example of how to make a simple GET request:
import 'package:http/http.dart' as http;
void makeRequest() async {
var url = Uri.parse('https://api.example.com/data');
var response = await http.get(url);
if (response.statusCode == 200) {
// Request successful, you can access the response body
print(response.body);
} else {
// Request failed
print('Request failed with status: ${response.statusCode}');
}
}
In this example, we import the http
package and use the get
method to make a GET request to the specified URL. We await the response and check the status code to determine if the request was successful. If the status code is 200, we can access the response body using response.body
. Otherwise, we handle the failure accordingly.
Please note that you need to add the http
package to your pubspec.yaml
file and run flutter pub get
to download the package before using it.
I hope this helps! Let me know if you have any further questions.