Using packages _ Flutter
Using packages _ Flutter
Learn more
Using packages
Packages & plugins chevron_right Using packages
Contents keyboard_arrow_down
Using packages
Searching for packages
Adding a package dependency to an app using flutter pub add
more_horiz
Flutter supports using shared packages contributed by other developers to the Flutter and
Dart ecosystems. This allows quickly building an app without having to develop everything
from scratch.
Packages
At a minimum, a Dart package is a directory containing a pubspec.yaml file.
Additionally, a package can contain dependencies (listed in the pubspec), Dart
libraries, apps, resources, tests, images, fonts, and examples. The pub.dev site lists
many packages—developed by Google engineers and generous members of the
Flutter and Dart community— that you can use in your app.
Plugins
A plugin package is a special kind of package that makes platform functionality
available to the app. Plugin packages can be written for Android (using Kotlin or
Java), iOS (using Swift or Objective-C), web, macOS, Windows, Linux, or any
combination thereof. For example, a plugin might provide Flutter apps with the
ability to use a device's camera.
Packages versus Plugins? | Decoding Flutter
Existing packages enable many use cases—for example, making network requests ( http ),
navigation/route handling ( go_router ), integration with device APIs ( url_launcher and
battery_plus ), and using third-party platform SDKs like Firebase (FlutterFire).
To write a new package, see developing packages. To add assets, images, or fonts,
whether stored in files or packages, see Adding assets and images.
Using packages
The following section describes how to use existing published packages.
The Flutter landing page on pub.dev displays top packages that are compatible with
Flutter (those that declare dependencies generally compatible with Flutter), and supports
searching among all published packages.
The Flutter Favorites page on pub.dev lists the plugins and packages that have been
identified as packages you should first consider using when writing your app. For more
information on what it means to be a Flutter Favorite, see the Flutter Favorites program.
You can also browse the packages on pub.dev by filtering on Android, iOS, web, Linux,
Windows, macOS, or any combination thereof.
1. Use the pub add command from inside the project directory
2. Import it
1. Depend on it
Open the pubspec.yaml file located inside the app folder, and add
css_colors: ^1.0.0 under dependencies .
2. Install it
3. Import it
1. Use the pub remove command from inside the project directory
flutter pub remove css_colors
The Installing tab, available on any package page on pub.dev, is a handy reference for
these steps.
Conflict resolution
Suppose you want to use some_package and another_package in an app, and both of
these depend on url_launcher , but in different versions. That causes a potential
conflict. The best way to avoid this is for package authors to use version ranges rather
than specific versions when specifying dependencies.
yaml
dependencies:
url_launcher: ^5.4.0 # Good, any version >= 5.4.0 but < 6.0.0
image_picker: '5.4.3' # Not so good, only version 5.4.3 works.
For example, to force the use of url_launcher version 5.4.0 , make the following
changes to the app's pubspec.yaml file:
yaml
dependencies:
some_package:
another_package:
dependency_overrides:
url_launcher: '5.4.0'
If the conflicting dependency is not itself a package, but an Android-specific library like
guava , the dependency override declaration must be added to Gradle build logic instead.
To force the use of guava version 28.0 , make the following changes to the app's
android/build.gradle file:
groovy
configurations.all {
resolutionStrategy {
force 'com.google.guava:guava:28.0-android'
}
}
Package versions
All packages have a version number, specified in the package's pubspec.yaml file. The
current version of a package is displayed next to its name (for example, see the
url_launcher package), as well as a list of all prior versions (see url_launcher
versions).
To ensure that the app doesn't break when you update a package, specify a version range
using one of the following formats.
yaml
dependencies:
url_launcher: '>=5.4.0 <6.0.0'
Ranged constraints using the caret syntax: Specify the version that serves as the
inclusive minimum version. This covers all versions from that version to the next
major version.
yaml
dependencies:
collection: '^5.4.0'
This syntax means the same as the one noted in the first bullet.
To upgrade to a new version of the package, for example to use new features in that
package, run flutter pub upgrade to retrieve the highest available version of the
package that is allowed by the version constraint specified in pubspec.yaml . Note that
this is a different command from flutter upgrade or flutter update-packages ,
which both update Flutter itself.
Path dependency
A Flutter app can depend on a package using a file system path: dependency. The
path can be either relative or absolute. Relative paths are evaluated relative to the
directory containing pubspec.yaml . For example, to depend on a package, packageA,
located in a directory next to the app, use the following syntax:
yaml
dependencies:
packageA:
path: ../packageA/
Git dependency
You can also depend on a package stored in a Git repository. If the package is located at
the root of the repo, use the following syntax:
yaml
dependencies:
packageA:
git:
url: https://github.com/flutter/packageA.git
yaml
dependencies:
packageA:
git:
url: [email protected]:flutter/packageA.git
yaml
dependencies:
packageA:
git:
url: https://github.com/flutter/packages.git
path: packages/packageA
Finally, use the ref argument to pin the dependency to a specific git commit, branch,
or tag. For more details, see Package dependencies.
Examples
The following examples walk through the necessary steps for using packages.
3. Run flutter pub get in the terminal, or click Get Packages in VS Code.
dart
import 'package:css_colors/css_colors.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
@override
Widget build(BuildContext context) {
return const MaterialApp(home: DemoPage());
}
}
@override
Widget build(BuildContext context) {
return Scaffold(body: Container(color: CSSColors.orange));
}
}
yaml
dependencies:
flutter:
sdk: flutter
url_launcher: ^5.4.0
3. Run flutter pub get in the terminal, or click Get Packages get in VS Code.
4. Open lib/main.dart and replace its full contents with the following:
dart
import 'package:flutter/material.dart';
import 'package:path/path.dart' as p;
import 'package:url_launcher/url_launcher.dart';
void main() {
runApp(const MyApp());
}
@override
Widget build(BuildContext context) {
return const MaterialApp(home: DemoPage());
}
}
void launchURL() {
launchUrl(p.toUri('https://flutter.dev'));
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ElevatedButton(
onPressed: launchURL,
child: const Text('Show Flutter homepage'),
),
),
);
}
}
5. Run the app (or stop and restart it, if it was already running before adding the
plugin). Click Show Flutter homepage. You should see the default browser open on
the device, displaying the homepage for flutter.dev.
Except as otherwise noted, this site is licensed under a Creative Commons Attribution 4.0 International
License, and code samples are licensed under the 3-Clause BSD License.