“This is the third day of my participation in the November Gwen Challenge. See details of the event: The last Gwen Challenge 2021”.

Yaml, pubSpec. lock, and analysis_options.yaml files are included in a new project created by flutter create XXX. In this article, we’ll explore the pubspec.yaml file – what it is and what we can do with it.

When we create a Flutter Project, we usually have the following file structure:

├ ─ ─ android ├ ─ ─ ios ├ ─ ─ lib ├ ─ ─ pubspec. Yaml ├ ─ ─ pubspec. Lock ├ ─ ─ analysis_options. Yaml ├ ─ ─ the test ├ ─ ─ web └ ─ ─ the README, mdCopy the code

So what is in pubspec.yaml? Let’s click on 😁 :

# General Section (Metadata) name: new_test description: A new Flutter project. publish_to: 'none' version: ">=2.12.0 <3.0.0" Flutter cupertino_icons: ^1.0.2 dev_dependencies: flutter_test: SDK: flutter_lints: ^1.0.0 # Flutter specific Configurations Flutter: uses-material-design: trueCopy the code

Basic information – Metadata

Name. This field indicates the package name and how we will import files in or from this project. For example, if we were to import the main.dart function in a file, we would import it as follows:

import 'package:new_test/main.dart';
Copy the code

However, if we change the name field to new_APP in the future, we must ensure that all imports are changed from new_test to new_app, which means our main.dart import will look like this:

import 'package:new_app/main.dart';
Copy the code

Description, as the name suggests, allows us to add a short description of the project (this field is optional). If we are creating a library, then if we decide to publish our package on [pub.dev](https://app pub.dev), then everyone can see this description.

Version will allow you to add semantic versioning to your application or library. For mobile applications, this will be the version that appears in the various app stores. A Flutter application 1.2.3+4 with version means that it is version 4 with build version 1.2.3. On the other hand, if we are creating a library, version control will enable any user to specify which version of the library they want to use and let them decide whether to use it or not. Dio: ^4.0.1 or DIO: 3.0.10. Additionally, if we create a library project, there are additional optional fields such as Author, homepage, issue_tracker, and Repository.

author: xxx
repository: https://github.com/felangel/bloc/tree/master/packages/flutter_bloc   
issue_tracker: https://github.com/felangel/bloc/issues
homepage: https://bloclibrary.dev                       
Copy the code

These fields will also be shown in * Metadata* to the right of pub.dev:

Environment

This field allows us to add constraints to both Dart SDK and Flutter Version:

Environment: the SDK: "> = 2.12.0 < 3.0.0"Copy the code

This means that this Project will only run higher than or equal to 2.12.0 but lower than 3.0.0 on the Dart SDK version. We don’t care which particular version it is currently using, just that it stays within bounds.

Additionally, we can specify which version of Flutter we will use:

Environment: the SDK: "> = 2.12.0 < 3.0.0 flutter: 2.5.0"Copy the code

In this case, if we use Flutter version 2.5.0. If we try to get our package using any other flutter version via flutter pub get, we will see the following error:

➜ example flutter pub get The current flutter SDK version is 2.1.0. Because example requires flutter SDK version 2.5.0, version solving failed. Running "flutter pub get" in example... pub get failed (1; Because example requires Flutter SDK version 2.5.0, Version solving failed.Copy the code

Dependencies

Dependencies and dev_dependencies contain all the packages we will use in our application. Ependencies: A package that anyone using your package will need. Dependencies that are needed only during the development phase of the package itself are listed in dev_dependencies. During development, you may need to temporarily override dependencies. You can use dependency_overrides.

When declaring dependencies on a particular package, we must first know the different ways to add them:

fromPub. DevManaged package dependencies

We go to pub.dev, select a package we need, such as Flutter_bloc, and go to the Install section to see how to add it to our project – add dependencies to the file in that Dependencies section by directly pubspec.yaml:

Dependencies: flutter_bloc: ^ 7.3.3Copy the code

Dependencies from the path directory

When we need to write a package of our own or for some online package custom rectification. You need to compile the local version. To do this we must use the path reference.

dependencies: bloc: path: .. /custom_flutter_blocCopy the code

Dependencies from git repositories

dependencies:
   bloc:
   	git:
   		url: https://github.com/felangel/bloc/tree/master/packages/flutter_bloc.git
   		ref: flutter_bloc_custom
   		path: packages/flutter_bloc
Copy the code
  • urlisgitRepository address
  • refCan beSubmit a hash,The label,branch(see Git).
  • pathYes, if the Git repository has several packages to use. If we look atblocThe official Github repository, which we’ll see insidepackagesIs that allblocRelated items, includingblocandflutter_bloc.

Dependencies from the custom publisher

Finally, when submitting our own packages or custom packages to our own Pub server, we must let PubSpec know that we are using a different PUB server. This can easily be done by adding the Hosted parameter:

Dependencies: Bloc: Hosted: name: Flutter_bloc URL: http://your-package-server.com version: ^6.0.0Copy the code

dependency_overrides

Imagine the following scenario: We use the flutter_bloc package 7.3.2 with version, but at the same time we rely on another package 6.1.3 with version. If we try to get an application dependency, we receive the following error:

➜  example: flutter pub get
Because main_app depends on package from path which depends on flutter_bloc 6.1.3, flutter_bloc 6.1.3 is required.
So, because main_app depends on flutter_bloc 7.3.2, version solving failed.
Running "flutter pub get" in main_app...                                
pub get failed (1; So, because main_app depends on flutter_bloc 7.3.2, version solving failed.)
Copy the code

We can override the dependency by using the dependency_overrides method:

Dependencies: Flutter_bloc: 7.3.2 Package: path:.. / package dependency_overrides: flutter_bloc: 7.3.3Copy the code

Summary: Our package uses Flutter_Blocversion 6.1.3 and our application uses Version 7.3.2. However, we need to run our application 7.3.3 using Version, so we override the dependencies. By doing this, when PubSpec compiles the list of all dependencies, it will use Version 7.3.3, regardless of what each application or library specifies:

When we retrieve the package with this new field, we see a new warning message indicating that we are using overridden dependencies:

➜ example: flutter pub get Warning: You are using these overridden dependencies:! Flutter_bloc 6.0.6 Running "flutter pub get" in main_app...Copy the code

But be warned. Dependency_overrides will only be used when we compile the application. This means that if we have a dependency_overrides on part of our package, and we compile the Example application, the Example application will ignore all other library dependency overrides.

Flutter part

At the bottom of our file, we see a file named flutter. When creating a new project, we see that it already has a parameter uses-material-design:

flutter:
  uses-material-design: true
Copy the code

Here is where we configure Flutter, such as resources and fonts, etc. :

flutter:
  uses-material-design: true

  assets:
    - images/icon_home_add.jpeg
    - images/icon_home_delete.jpeg

  fonts:
    - family: Schyler
      fonts:
        - asset: fonts/Schyler-Italic.ttf
          style: italic
          weight: 700
Copy the code

It is also used to set the plug-in version for each platform when creating the plug-in package.

flutter:
  plugin:
    platforms:
      android:
        package: io.flutter.plugins.xwuppay
        pluginClass: XWUPPayPlugin
      ios:
        pluginClass: XWUPPayPluginPlugin
      macos:
        default_package: xwuppay_macos
      web:
        default_package: xwuppay_for_web
Copy the code

The last

The pubspec.yaml file is horizontal for all applications and packages — it is where we add metadata to the project, specify Dart and Flutter SDK constraints, manage dependencies, and set up Flutter specific configurations.

However, one thing we don’t see is what happens after we get the package (flutter pub get e.g. pass). Specifically what happens to the pubspec.lock file. This will be the subject of a future article, where we will see, among other things, what symbol ^ uses when declaring pub.dev dependencies and why we should or should not use it. These will be explained in the next chapter.

For a list of all The fields we can use, and for more information on fields, check out The official The PubSpec File