The pubspec.yaml file is an important configuration file for Flutter.

Yaml is the Flutter project configuration file, similar to the Android Gradle configuration file. Let’s take a look at the configuration of pubspec.yaml properties.

Create a new Flutter Application with pubspec.yaml in the root directory, as shown in the following figure:

In the default configuration of the project, remove the comment part, leaving the following:

name: flutter_app
description: A new Flutter application.

publish_to: 'none' 

version: 1.0. 0+1

environment:
  sdk: "> = 2.7.0 < 3.0.0"

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0. 0

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:
  uses-material-design: true

Copy the code

Let’s go through them one by one.

name

This property represents the package name, which is important enough to be used when importing other files:

import 'package:flutter_app/home_page.dart';
Copy the code

If you change the package name, the corresponding import also needs to be changed:

import 'package:flutter_app_demo/home_page.dart';
Copy the code

If you create a Flutter plugin and publish it to pub.dev, this property will be displayed as a title and other references will need to use this property.

description

The Description property is an optional configuration property and is an introduction to the current project. If published as a plug-in to pub.dev, this value appears as follows:

version

The version and internal version number of this property application in the format of X.X.X +x, for example: 1.0.0+1. This version is called Semantic versioning. See here for semantic versioning information.

The version number is separated by two dots, and the following part is called build number.

In Android, version number corresponds to versionName and build number corresponds to versionCode. In Android /build.gradle, there are related configurations.

In earlier versions of build.gradle, versionName and versionCode are written directly to dead numbers as follows:

In this case, you can change the version number only by modifying build.gradle. Now you can modify it directly through pubspec.yaml.

If it is a plug-in, then the user can specify which version to use with this version number,

path_provider: ^1.622.
Copy the code

Version specification takes many forms:

Do not specify or any

path_provider:

path_provider: any
Copy the code

This format loads the latest version by default, but it is strongly not recommended because version changes may cause interface changes and project compilation exceptions.

x.y.z

Explicitly specifying the version

path_provider: 1.622.
Copy the code

Specify the version of the dependency.

< = X.Y.Z or < X.Y.Z

Less than or equal to this version of the package

Path_provider: < = 1.6.22 path_provider: < 1.6.22Copy the code

>=a.b.c <x.y.z

Specifies the range of versions

path_provider: '> = 1.0.0 < 1.6.22'
Copy the code

^x.y.z

This method is the most common and recommended method.

For example, ^1.6.22 is equivalent to ‘>=1.6.22 <2.0.0’

path_provider: ^1.622.
Copy the code

author homepage issue_tracker repository

These four properties are not available in the Flutter Application project by default and are not required in the normal project. They are required when we develop the plugin and publish it to pub.

When we create a plug-in, the default configuration is:

Issue_tracker and Repository can be created manually. These four attributes illustrate:

  • Author: The author, fill in your signature
  • Homepage: homepage.
  • Issue_tracker: issue, the Github issue address for writing the current plug-in source code.
  • Repository: The usual Github address for writing the source code of the current plug-in.

These properties are displayed on the pub.dev home page:

Environment

Added Flutter and Dart version control under the Environment property.

environment:
  sdk: "> = 2.7.0 < 3.0.0"
Copy the code

The above version specifies that this application or library can only run on Dart SDK versions 2.7.0 or higher and 3.0.0 or lower.

We can also manually add the Flutter version:

environment:
  sdk: "> = 2.7.0 < 3.0.0"
  flutter: "1.22.0"
Copy the code

You can also use an experimental version with this property:

environment:
  sdk: "> = 2.11.0-213.0. Dev < 2.12.0"
Copy the code

Dependencies and dev_dependencies

Dependencies and dev_dependencies contain the packages on which the application depends. Dependencies and dev_dependencies are just like their names. All dependencies under dependencies are compiled into the project. Dev_dependencies are only run-time packages, such as libraries that automatically generate code.

We can rely on its packages in four ways:

  • Rely on third-party libraries on pub.dev
  • Dependency on local libraries
  • Relying on the git repository
  • Rely on our own PUB warehouse

Rely on third-party libraries on pub.dev

Relying on third-party libraries on pub.dev is the most common way to do this

dependencies:
  path_provider: ^1.622.
Copy the code

Dependency on local libraries

If you create a module locally and rely on the local library:

dependencies: flutter_package: path: .. /flutter_packageCopy the code

Relying on the git repository

Rely on a plugin on Github:

dependencies:
  bloc:
    git:
      url: https://github.com/felangel/bloc.git
      ref: bloc_fixes_issue_110
      path: packages/bloc
Copy the code
  • Url: Github address
  • Ref: git reference, which can be a commit hash, tag, or branch
  • Path: You can use this property to specify packages if you have multiple packages in your Git repository

Rely on our own PUB warehouse.

General large companies will build their own pub warehouse, citing their own warehouse:

dependencies:
  bloc: 
    hosted:
      name: bloc
      url: http://your-package-server.com
    version: ^6.0. 0
Copy the code

Depend on the cover

Imagine the following scenario: The project relies on a library (e.g., path_provider) with version 1.6.22, while another library relies on the same library (version 0.5.0). Which one should I rely on?

Running "flutter pub get" in flutter_app...                     

Because every version of flutter_plugin from path depends on path_provider ^0.5.0 and flutter_app depends on path_provider ^1.6.22, flutter_plugin from path is forbidden.
So, because flutter_app depends on flutter_plugin from path, version solving failed.
pub get failed (1; So, because flutter_app depends on flutter_plugin from path, version solving failed.)
Process finished with exit code 1
Copy the code

Dependency_overrides to resolve the conflict:

dependency_overrides:
  path_provider: ^1.622.
Copy the code

When this property is added, all path_Provider plug-ins will use the same latest version. Using this field to execute the flutter pub GET will cause the following warning:

/Users/mengqingdong/project/flutter/bin/flutter --no-color pub get
Running "flutter pub get" in flutter_app...                     

Warning: You are using these overridden dependencies:
! path_provider 1.6.22
Running "flutter pub get" in flutter_app...                         0.5s
Process finished with exit code 0
Copy the code

Flutter

The configuration of Flutter is related to Flutter.

uses-material-design

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

Make sure you include the Material Icons font in your application so that you can use the Icons from the Material Icons class.

assets

Assets are the configuration of current resources, such as images, fonts, etc.

Configure the local Image and load it using image.asset ().

assets:
  - images/a_dot_burr.jpeg
  - images/a_dot_ham.jpeg
Copy the code

Configuration font:

fonts:
  - family: Schyler
    fonts:
      - asset: fonts/Schyler-Regular.ttf
      - asset: fonts/Schyler-Italic.ttf
        style: italic
  - family: Trajan Pro
    fonts:
      - asset: fonts/TrajanPro.ttf
      - asset: fonts/TrajanPro_Bold.ttf
        weight: 700
Copy the code

plugin

Plugin configurations only exist in plug-in projects, packages and pluginClass generally do not need to be modified.

flutter:
  plugin:
    platforms:
      android:
        package: com.flutter.app_market
        pluginClass: AppMarketPlugin
      ios:
        pluginClass: AppMarketPlugin
Copy the code

This configuration does not need to be modified normally. To add a new platform adaptation, add the following:

flutter:
  plugin:
    platforms:
      android:
        package: com.flutter.app_market
        pluginClass: AppMarketPlugin
      ios:
        pluginClass: AppMarketPlugin
      macos:
        default_package: app_market_macos
      web:
        default_package: app_market_web
        
Copy the code

Pubspec.yaml contains applications and dependent packages, specifies Dart and Flutter SDK version constraints, manages dependencies, and sets Flutter specific configurations. More detailed information can be found in pubSpec’s official documentation.

communication

Lao Meng Flutter blog (330 controls usage + practical primer series) : laomengit.com