2

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 "..." ?

edn
  • 1,981
  • 3
  • 26
  • 56

3 Answers3

10

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!

Apoorv Pandey
  • 2,118
  • 2
  • 11
  • 13
3

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.

Tim Klingeleers
  • 2,834
  • 14
  • 20
1

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.

Bilaal Abdel Hassan
  • 1,181
  • 2
  • 12
  • 20
  • That is great information. But am I supposed to put versions between double quotes? Is there any recommended way? – edn Dec 18 '20 at 15:37
  • 1
    I have seen tons of codes and usually it came down to user preference. I haven't seen any preferred convention so far. Maybe someone else can say more about that :) – Bilaal Abdel Hassan Dec 18 '20 at 16:45