Sub topic 12
Adding a Package to a Flutter App
Assume you know how to create and run a Flutter project, read pubspec.yaml, and work with Dart classes and asynchronous functions. This chapter shows how to add an external package and use it safely from your app.
What a package provides
A package is reusable Dart or Flutter code maintained outside your application. Packages can provide networking, database access, device integration, state management, testing utilities, and more.
Flutter packages are declared in pubspec.yaml. The dependencies section lists packages required by the running application. Flutter uses Dart’s package manager, called pub, to resolve compatible versions and download them.
| File or command | Purpose |
|---|---|
| `pubspec.yaml` | Declares package names and version constraints |
| `flutter pub add http` | Adds a package and resolves its dependencies |
| `flutter pub get` | Installs dependencies declared in the manifest |
| `pubspec.lock` | Records the exact resolved versions |
Add the `http` package
From the project directory, run:
flutter pub add http
flutter pub getThe first command updates pubspec.yaml for you. The second command is often run automatically, but it is useful when the manifest has been edited manually or when dependencies need refreshing.
You can also inspect the package after adding it:
dependencies:
flutter:
sdk: flutter
http: ^1.2.2The caret allows compatible releases according to Dart’s version rules. Do not copy a version blindly from an old tutorial. Use the version that flutter pub add selects, or check the package’s current documentation.
Import and use a package
The package name becomes the import prefix. This example fetches JSON from a public endpoint and displays the response status. Replace the generated lib/main.dart with this complete example:
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: StatusPage(),
);
}
}
class StatusPage extends StatefulWidget {
const StatusPage({super.key});
@override
State<StatusPage> createState() => _StatusPageState();
}
class _StatusPageState extends State<StatusPage> {
String message = 'Press the button to request data.';
Future<void> loadData() async {
setState(() {
message = 'Loading...';
});
final response = await http.get(
Uri.parse('https://jsonplaceholder.typicode.com/todos/1'),
);
if (!mounted) return;
setState(() {
message = 'HTTP status: ${response.statusCode}';
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Package example')),
body: Center(child: Text(message)),
floatingActionButton: FloatingActionButton(
onPressed: loadData,
child: const Icon(Icons.download),
),
);
}
}http.get returns a Future<Response>, so loadData is asynchronous. The mounted check prevents a state update if the page was removed while the request was running.
Understand dependency changes
When you add a package, Flutter may update both pubspec.yaml and pubspec.lock. Commit the lock file for an application so other developers and build systems resolve the same versions. Do not manually edit the lock file to fix a conflict. Change the version constraint in pubspec.yaml, then run flutter pub get.
Exercise
Why should an application commit pubspec.lock after adding a package?
Show answer
It records the resolved dependency versions, making local, CI, and release builds more reproducible.