Complete the code to fetch data from a remote API using Flutter's http package.
final response = await http.[1](Uri.parse('https://api.example.com/data'));
The get method is used to fetch data from a remote API in Flutter.
Complete the code to decode the JSON response body after fetching remote data.
final data = json.decode(response.[1]);The body property contains the raw JSON string from the response, which we decode.
Fix the error in the code by choosing the correct way to handle asynchronous API calls in Flutter.
void fetchData() {
http.get(Uri.parse('https://api.example.com/data')).[1]((response) => print(response.body));
}The get method returns a Future. Use then to handle the response asynchronously.
Fill both blanks to create a function that fetches data asynchronously and returns the decoded JSON.
Future<dynamic> fetchData() async {
final response = await http.[1](Uri.parse('https://api.example.com/data'));
return json.[2](response.body);
}Use get to fetch data and decode to convert JSON string to a Dart object.
Fill all three blanks to handle errors when fetching remote data and decoding JSON.
Future<dynamic> fetchData() async {
try {
final response = await http.[1](Uri.parse('https://api.example.com/data'));
if (response.statusCode == 200) {
return json.[2](response.[3]);
} else {
throw Exception('Failed to load data');
}
} catch (e) {
return null;
}
}Use get to fetch, decode to parse JSON, and body to access the response content.