Google Flutter - Introduction to Dependencies & Packages

Google, Google Flutter, google packages -

Google Flutter - Introduction to Dependencies & Packages

Introduction

In Dart, you don’t have to develop everything from scratch. There is a packaging system where developers can develop packages and publish them. Other people can then use these packages.

Website

When someone writes a package and it is published to the https://pub.dartlang.org/ site, developers can declare a dependency to that project and pull it into their project as a dependency. Then the user can add imports at the top the files to import code and use it. Note that Dart and Flutter packages follow semantic versioning rules.

Core Packages

Flutter comes with many packages by default. These are called Core Packages and you don’t need to declare any kind of external dependency to use them.

Non-Core Packages

You could call these ‘External Packages’. These are packages that are not setup by default. You need to declare these dependencies and pull them into your project to use them.

Most Useful Non-Core Packages

These are the packages that I have used the most. This may be very different for other Flutter developers.

Name Description
http For HTTP communication.
rxdart Reactive functional programming library.
datetime_picker_formfield Date / time picker.
image_picker Image picker. Very useful apps where you take pictures or upload photos.
zoomable_image For panning and zooming images by touch.
shared_preferences For saving local settings and data in your app.
cached_network_image A flutter library to show images from the internet and keep them in the cache directory. This helps speed things up. It also lets you display an image placeholder while the image loads.

How to Use an External Package

1. Declare Dependency in Project

Open the pubspec.yaml file in the root of your project and add a dependency. For example, the code below declares dependencies to the flutter sdk, cupertino icons and scoped_model. Note how some dependencies specify the version, some don’t:

  
  flutter:
    sdk: flutter
  cupertino_icons: ^0.1.2
  scoped_model: ^1.0.1

2. Import Packages

Once your pubspec.yaml file is setup, you need to install the packages by pulling them from https://pub.dartlang.org/. Normally your editor will assist you with this.

  • Android Studio. Click ‘Packages Get’ in the action ribbon at the top of pubspec.yaml.
  • Visual Studio Code. Click ‘Get Packages’ located in right side of the action ribbon at the top of pubspec.yaml.
  • Command-Line.  Run the command ‘flutter packages get’.

3. Import & Use Package Code

You import the package code in the usual manner using the ‘import’ statement at the top of your code. For example, the code imports the flutter material package and the scoped model package.

  
import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';

4. Restart Your App

You will probably need to restart your app if it is running.

Package Version Numbers

Syntaxes

  • Version specifiers.
    • ‘any’ – any version
    • ‘1.2.3’ – only version 1.2.3
    • ‘>1.8.3’ – any version higher than 1.8.3
    • '>=1.8.3’ – any version 1.8.3 or higher
    • ‘<1.8.3’ – any version lower than 1.8.3
    • '<=1.8.3’ – any version 1.8.3 or lower
  • Carat syntax.
    • The ‘^’ means - “the range of all versions guaranteed to be backwards compatible with the specified version”.
    • ‘1.2.3’ – only version 1.2.3
    • ‘^1.1.1’ is equivalent to versions '>=1.1.1 <2.0.0'< /li>
    • ‘^0.1.2’ is equivalent to versions '>=0.1.2 <0.2.0'< /li>

Latest Version Number

Sometimes you want to use a package, but you don’t know the latest version number of that package. In this case you have a couple of options.

  • You can find the package’s version here: https://pub.dev/flutter/packages.
  • You can import the dependency into the project then review the version number:
  • Add the dependency in the pubspec.yaml file without a version number.
  • Use the ‘flutter packages get’ to get the packages for the project. Flutter will default to the latest version number for your new dependency.
  • You can then review the pubspec.lock file to see what version was used.
  • Specify that version for the dependency in the pubspec.yaml to ensure it uses that version going forward.