CodeBlanks Logo Icon CodeBlanks

http.get

Fetch data with http.get.

What is http.get?

http.get is a method used to fetch data from a URL. It takes a Uri and returns a Response.

final response = await http.get(
  Uri.parse('https://api.example.com/data'),
);
print(response.body);

Response carries the returned data and the statusCode.

Uri.parse and Response

You convert the URL string to a Uri object with Uri.parse. The Response object carries the returned data and the status code.

final uri = Uri.parse('https://api.example.com/data');
final response = await http.get(uri);
print(response.body);

response.body gives the returned content as a string.

Practice Fetch data with http.get

Write an async method veriCek. Fetch data with http.get(Uri.parse('https://api.example.com/veri')) and print response.body with print('gövde: ${response.body}'). In main, await veriCek and call print('istek tamam').

Starter code

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

// veriCek: async metot ekle

void main() async {
  // veriCek'i çağır ve mesaj yazdır
}

Sample solution

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

Future<void> veriCek() async {
  final response = await http.get(Uri.parse('https://api.example.com/veri'));
  print('gövde: ${response.body}');
}

void main() async {
  await veriCek();
  print('istek tamam');
}

Expected output

gövde: {"isim":"Ayşe"}
istek tamam

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What parameter is given to http.get?

  • An int
  • A String directly
  • A Uri object Correct answer
  • A Map

Explanation: http.get takes a Uri object; the URL string is converted to a Uri with Uri.parse.

2 Convert the URL to a Uri

Question
final uri = ____.parse('https://api.example.com/veri');
final response = await http.get(uri);
Answer
final uri = Uri.parse('https://api.example.com/veri');
final response = await http.get(uri);

Explanation: The Uri.parse method converts a URL string into a Uri object; http.get takes this Uri.

3 http.get parts

Term Meaning
http.get Data fetching method
Uri.parse Method converting URL to Uri
Response Request return object
body Property giving returned content

All lessons in Intermediate