Sub topic 7
Importing a Package and Calling Its API
Assume you know basic Dart syntax, can run terminal commands, and have already added the http package to your Flutter project's pubspec.yaml. This chapter shows how an import connects your Dart code to a package's public API.
Add the import
An import is a Dart statement that makes declarations from another library available in a file. A package import starts with package: and uses the package name:
import 'package:http/http.dart' as http;The as http part creates a prefix. You use that prefix when calling package functions, such as http.get. A prefix makes the source of each function clear and prevents naming conflicts.
The package name must match the name in pubspec.yaml. The package must also be resolved successfully before the import works.
Call an HTTP API
The http package provides functions for sending HTTP requests. The following complete Dart program sends a GET request and prints the response body:
import 'package:http/http.dart' as http;
Future<void> main() async {
final uri = Uri.parse('https://example.com');
try {
final response = await http.get(uri);
if (response.statusCode == 200) {
print(response.body);
} else {
throw Exception(
'Request failed with status ${response.statusCode}',
);
}
} catch (error) {
print('Could not load the page: $error');
}
}Uri represents a Uniform Resource Identifier, such as a web address. Uri.parse converts a string into a Uri object.
http.get starts an asynchronous operation. An asynchronous operation can finish later, such as after a network response arrives. It returns a Future<Response>, where Future represents a value that will be available later and Response is the package's response type.
The await keyword pauses this function until the request finishes. Because the function uses await, its declaration includes async.
The response contains several useful properties:
statusCodeis the server's numeric result, such as200for success or404for not found.bodyis the response content as a string.headerscontains metadata sent with the response.
A successful HTTP request does not always mean the server returned useful application data. Check the status code before using the body.
Understand the call flow
The import, request, response, and error paths form this sequence:
Avoid common mistakes
- Importing
httpwithout adding it topubspec.yaml. - Forgetting the
http.prefix when the import usesas http. - Calling asynchronous code without
await. - Treating every response as successful.
- Updating the user interface directly from a network function instead of returning data and letting the widget decide how to display it.
A useful design is to put package calls in a separate service function. The user-interface code can then call that function, show a loading state, and display either the returned data or an error.
Exercise
What does await http.get(uri) produce, and why does the surrounding function need the async keyword?
Show answer
It produces the completed HTTP response after the Future finishes. The function needs async so it can use await while the network operation completes.