Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In the Flutter what is best way to add package in pubspec.yaml file?

Tags:

flutter

dart

In flutter I'm little bit concussion to add package.

Rather than add package with version, is it best way to add package without version in pubspec.yaml file?

Might be, By default it will acquire latest version. But what happen when after adding new version will be available?

like image 659
Govaadiyo Avatar asked Jan 28 '23 00:01

Govaadiyo


1 Answers

You can omit the version or use any, but it's a good idea to add a version range.

Avoid updates breaking your app/package

Specifying a version constraint helps to avoid unexpected breaking your app by running flutter packages get when new dependency versions become available that contain breaking changes (are not compatible with your old code).
You can then intentionally extend the version range for a dependency when you align your code to the new version of the dependency.

Dart and packages are supposed to follow Semantic Versioning which means when an update contains a breaking change, the major version number needs to be incremented.

For versions below 1.0.0 incrementing the minor version number indicates a breaking change.

The ^ is a shortcut to define a version range that indicates the defined version and all later versions that don't contain breaking changes.

So usually you would use

some_dependency: ">=2.0.0 <3.0.0"

or short

some_dependency: ^2.0.0

If some update fixes a bug in 2.1.0 that your application or package depends on you can use

some_dependency: ">=2.1.0 <3.0.0"

or short

some_dependency: ^2.1.0

Performance

Specifying a narrow version constraint also can make flutter packages get/upgrade faster especially when your application contains a lot of dependencies because this reduces the search space for packages get/upgrade that it needs to traverse to find a compatible set of dependencies.

Missing functionality

Please upvote https://github.com/flutter/flutter/issues/12627 to get proper information from flutter packages get/upgrade when newer dependency versions are available than your constraints allow (like pub get/upgrade does for non-Flutter Dart projects)

See also

  • https://www.dartlang.org/tools/pub/versioning
  • What does plus one (+1) mean in dart's dependency versioning
like image 193
Günter Zöchbauer Avatar answered Feb 01 '23 18:02

Günter Zöchbauer