edn
edn

Reputation: 2183

How to set dependencies in the right way in pubspec.yaml in Flutter

Here are a few dependency examples from my pubspec.yaml file in my Flutter project.

dependencies:
  flutter:
    sdk: flutter
  cloud_firestore: ^0.14.4
  firebase_core_web: ^0.2.1
  firebase_crashlytics: "^0.2.4"
  firebase_analytics: "^6.3.0"

I just followed installation guidelines when installing each of them. As seen, some dependencies have the version number in "..." while others don't.

It seems to be working in both ways but I wonder what the right way of doing it. Should I always put the version number into "..." ?

Upvotes: 3

Views: 13639

Answers (3)

Apoorv Pandey
Apoorv Pandey

Reputation: 2511

The best way to do this is:

flutter pub add form_field_validator

where "form_field_validator" is the name of dependency

Have a look why it is a best method: It automatically adjusts all of the the dependencies

It automatically adjusts all of the the dependencies

So you don't get "version solving failed" error like this showing below: Because flutter_bloc: 0.21.0 depends on provider: ^3.0.0 and no versions of flutter_bloc match: >0.21.0 <0.22.0, flutter_bloc: ^0.21.0 requires provider: ^3.0.0. So, because it tells_me that it depends on both provider: ^4.1.2 and flutter_bloc: ^0.21.0, version solving failed.

https://pub.dev/packages/form_field_validator/install

has both methods copy and paste the given command in your project directory

See below image:

enter image description here

Thank you I hope it clears everything, happy coding!

Upvotes: 11

Tim Klingeleers
Tim Klingeleers

Reputation: 3024

You can provide version numbers with and without quotes. The quotes are used for providing range constraints on dependencies like this:

dependencies:
  url_launcher: '>=5.4.0 <6.0.0'

So that's why both options work. If you are not using ranges you can omit the quotes but it comes down to personal/team preference. See the Flutter documentation for more information on using packages.

Upvotes: 4

Bilaal Abdel Hassan
Bilaal Abdel Hassan

Reputation: 1379

If you don't put any number or version number, it takes the latest version.

When creating new projects, it will work good. However if you are re-using codes from other projects, you might want to use the exact same version of the dependencies, hence you define them.

Upvotes: 1

Related Questions