In this tutorial, we will learn how to use Flutter packages to enhance the functionality of our Flutter projects and improve our development speed. Packages are libraries of reusable code that can be integrated into our apps to perform specific tasks, saving us from writing these functionalities from scratch.
By the end of this tutorial, you'll be able to:
Prerequisites: You should have Flutter SDK and Dart installed on your system. Basic knowledge of Dart and Flutter is required.
You can find Flutter packages on the Dart package managing system, pub.dev. Here, you can search for packages and check their documentation, popularity, health, and maintenance.
To add a package to your project, you need to add it to your pubspec.yaml
file, which is the configuration file for your Flutter project.
dependencies:
flutter:
sdk: flutter
package_name: ^version_number
Replace package_name
and version_number
with the name and version of the package you want to add.
After adding the package, run flutter pub get
in the terminal to download the package.
Import the package at the beginning of your Dart file to use it.
import 'package:package_name/package_name.dart';
Replace package_name
with the name of the package.
Let's consider the http
package, which is used for networking in Flutter.
First, we add the package to our pubspec.yaml
file:
dependencies:
flutter:
sdk: flutter
http: ^0.13.3
We then run flutter pub get
in the terminal.
Next, we import the package in our Dart file:
import 'package:http/http.dart' as http;
We can now use the functionalities provided by the http
package. For example, to fetch data from an API:
Future<void> fetchData() async {
http.Response response = await http.get('https://api.example.com/data');
if (response.statusCode == 200) {
print('Data fetched successfully');
} else {
print('Failed to fetch data');
}
}
In this tutorial, we've learned how to search for, add, and use packages in a Flutter project. The next step would be exploring more packages and understanding how they can help speed up your development process.
Exercise 1: Find a package on pub.dev that allows you to make use of SQLite databases in Flutter. Add it to your project and make a simple query.
Exercise 2: Use the shared_preferences
package to store and retrieve simple data in your Flutter app.
Solutions:
sqflite
package can be used to interact with SQLite databases. After adding it to your project, you can make a simple query as follows:import 'package:sqflite/sqflite.dart';
void fetchData() async {
var database = await openDatabase('my_db.db');
List<Map> result = await database.rawQuery('SELECT * FROM MyTable');
print(result);
}
shared_preferences
package is used for persisting simple data. Here's how you can use it:import 'package:shared_preferences/shared_preferences.dart';
void storeData() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setInt('myNumber', 10);
}
void retrieveData() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
int myNumber = prefs.getInt('myNumber') ?? 0;
print(myNumber);
}
In the first function, we store an integer value in shared preferences. In the second function, we retrieve this value. If the value doesn't exist, we return 0 by default.