What is the http package?
- Flutter course
- Intermediate
- Lesson 46/99
- 4 min
Make REST requests with the http package.
What is the http package?
The http package is the official package used to make REST API requests in Flutter. With methods like get, post, put, delete you communicate with a server.
import 'package:http/http.dart' as http;
final response = await http.get(Uri.parse(url));All requests work asynchronously.
REST and http methods
REST is an architecture that works over the HTTP protocol. GET reads data, POST sends new data, PUT updates, DELETE deletes. The http package supports these methods directly.
await http.get(uri); // read
await http.post(uri); // send
await http.delete(uri); // deleteEach method returns a Response.
Practice Import the http package and print ready
Import the http package with as http. In main, call print('http hazır').
Starter code
import 'package:flutter/material.dart';
// http paketini içe aktar
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(home: Scaffold(body: Center(child: Text('http'))));
}
}Sample solution
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() {
print('http hazır');
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(home: Scaffold(body: Center(child: Text('http'))));
}
}Expected output
http hazır
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the http package for?
- To draw UI components
- To manage memory for file downloads
- For local database operations
- To make REST API requests Correct answer
Explanation: The http package is used to make REST API requests; it provides methods such as get, post, put, delete.
2 Import the http package
import 'package:http/http.dart' as ____;
import 'package:http/http.dart' as http;
Explanation: The http package is usually imported with the 'as http' alias; this way http.get, http.post are written.
3 http parts
| Term | Meaning |
|---|---|
http |
Package making REST requests |
REST |
HTTP-based architecture |
get |
Data reading method |
Response |
Request return object |