text
stringlengths
1
474
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.Ranged constraints: Specify a minimum and maximum version.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.This syntax means the same as the one noted in the first bullet.To learn more, check out the package versioning guide.<topic_end>
<topic_start>
Updating package dependencies
When running flutter pub get
for the first time after adding a package,
Flutter saves the concrete package version found in the pubspec.lock
lockfile. This ensures that you get the same version again
if you, or another developer on your team, run flutter pub get.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.<topic_end>
<topic_start>
Dependencies on unpublished packages
Packages can be used even when not published on pub.dev.
For private packages, or for packages not ready for publishing,
additional dependency options are available:Finally, use the ref argument to pin the dependency to a
specific git commit, branch, or tag. For more details, see
Package dependencies.<topic_end>
<topic_start>
Examples
The following examples walk through the necessary steps for
using packages.<topic_end>
<topic_start>
Example: Using the css_colors package
The css_colors package
defines color constants for CSS colors, so use the constants
wherever the Flutter framework expects the Color type.To use this package:Create a new project called cssdemo.Open pubspec.yaml, and add the css-colors dependency:Run flutter pub get in the terminal,
or click Get Packages in VS Code.Open lib/main.dart and replace its full contents with:
<code_start>import 'package:css_colors/css_colors.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: DemoPage(),
);
}
}
class DemoPage extends StatelessWidget {
const DemoPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(body: Container(color: CSSColors.orange));
}
}<code_end>
<topic_end>
<topic_start>
Example: Using the url_launcher package to launch the browser
The url_launcher plugin package enables opening
the default browser on the mobile platform to display
a given URL, and is supported on Android, iOS, web,
Windows, Linux, and macOS.
This package is a special Dart package called a
plugin package (or plugin),
which includes platform-specific code.To use this plugin:Create a new project called launchdemo.Open pubspec.yaml, and add the url_launcher dependency:Run flutter pub get in the terminal,
or click Get Packages get in VS Code.Open lib/main.dart and replace its full contents with the
following:
<code_start>import 'package:flutter/material.dart';
import 'package:path/path.dart' as p;
import 'package:url_launcher/url_launcher.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: DemoPage(),
);
}
}
class DemoPage extends StatelessWidget {
const DemoPage({super.key});
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'),
),