How to Use Flutter Packages

Tutorial 1 of 5

1. Introduction

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:

  • Search for Flutter packages.
  • Add packages to your Flutter project.
  • Use packages in your Flutter project.

Prerequisites: You should have Flutter SDK and Dart installed on your system. Basic knowledge of Dart and Flutter is required.

2. Step-by-Step Guide

Searching for Flutter Packages

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.

Adding a Flutter Package

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.

Using a Flutter 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.

3. Code Examples

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');
  }
}

4. Summary

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.

5. Practice Exercises

  1. 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.

  2. Exercise 2: Use the shared_preferences package to store and retrieve simple data in your Flutter app.

Solutions:

  1. The 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);
}
  1. The 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.