“This is the 24th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

One of the most important files in Flutter is the pubspec.yaml file, which is the configuration file for the Flutter project. It works like package.json in Node.js or Gradle files in Android.

Pubspec. yaml file location

We create a new Flutter project with the configuration file pubspec.yaml in the root directory, as shown below:

Pubspec. yaml is configured by default

In the new project, the default configuration in pubspec.yaml, without the comment part, is as follows:

name: flutter_demo
description: A new Flutter project.

publish_to: 'none' 

version: 1.0. 0+1

environment:
  sdk: "> = 2.12.0 < 3.0.0"

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.02.

dev_dependencies:
  flutter_test:
    sdk: flutter

  flutter_lints: ^1.0. 0

flutter:

  uses-material-design: true
Copy the code

Configuration Items

name

This property represents the package name. This property plays an important role in the entire configuration and will be used when importing other files written by us, such as:

name: flutter_demo
Copy the code

When we import other files, we need to use the following method:

import 'package:flutter_demo/listview_demo/listview_demo.dart';
Copy the code

If we modify the configuration of name as follows:

name: flutter_app
Copy the code

So, when we import the file, the imported import should also be modified accordingly:

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

Note that if we develop a Flutter plugin and publish it to the public, this property will be displayed as a title on the pub.dev website. This property will also be used when others use the Flutter plugin.

description

This property is an optional configuration item that provides an introduction to the current Flutter project. When released as a plugin, it will also appear on pub.dev as follows:

publish_to

Where does this property mean the package is published to?

  • none: indicates that the package is not released.
  • You can also specify the publishing server; As you can see from the comments, if this configuration is removed, it is published by default topub.dev

version

This attribute indicates the version of the current project, including the application version and internal version. The format is X.X.X +x, for example, 1.0.0+1, which is called the semantic version number.

  • +The one before the sign is calledversion number;
  • +The one after the sign is calledbuild number;

The build.gradle file in the andioid/app directory of the Flutter project has the following configuration:

Version number corresponds to versionName. Build number corresponds to versionCode;

environment

Flutter and Dart versions can be configured under this property. After we initialize the project, the SDK configured under this property is a range value representing the Dart version number, for example:

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

Represents the syntax of Dart versions whose engineering-compatible version number is greater than or equal to 2.12.0 and less than 3.0.0;

We can also manually add the version number of Flutter, for example:

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

dependencies

Under this attribute, we generally add the SDK of the third party we use, the default configuration is as follows:

dependencies:
  flutter:
    sdk: flutter
      
  cupertino_icons: ^1.02.
Copy the code
  • sdk: flutterGet by defaultflutterThe latest version, which is on our machineflutterVersion, which we can also add hereversionTo specify theflutterThe version of the;
dependencies:
  flutter:
    sdk: flutter
    version: "2.5.3"
Copy the code
  • cupertino_icons: Add to the applicationCupertinoIcon, generally used foriOS;

In a previous article, we introduced HTTP to send network requests, and we configured HTTP here as follows:

dependencies:
  flutter:
    sdk: flutter
      
  cupertino_icons: ^1.02.
  http: ^0.134.
Copy the code

The version numbers of these third parties can be written as follows:

  • Do not specify a/any): The latest version is loaded by default. However, this is generally not recommended, because the version change may cause the invocation method to change, resulting in project compilation errors;
  • x.y.z: Explicitly specify a version number to use;
  • <=x.y.z/<x.y.z: Uses packages whose version is less than or equal to a certain version. In this way, the version number should be quoted.
  • >=x.y.z <x.y.z: Specifies the version of a range to use. In this way, the version number is quoted.
  • ^x.y.z: This method is the most common and recommended method. meaningUse the latest minor version while the larger version remains unchanged; Such as:^ 2.12.0The equivalent of> = 2.12.0 < 3.0.0;

dev_dependencies

This configuration item is basically similar to the configuration of Dependencies, in that it is the package that the application depends on.

In addition, dependencies are compiled into the project. Dev_dependencies only configures runtime packages.

flutter

This configuration is generally Flutter related;

uses-material-design: true
Copy the code

The default configuration above is to make sure that we include the Material Icons font in our application so that we can use the Icons from the Material Icons class;

The configuration of resources is also set under this configuration:

  • assets: Configuration picture;
  • fonts: Configures the font.
  • plugin: This configuration only exists in the plug-in project and is used to configure the adaptation platform. Do not modify it. If you need to add a new platform, just add it.

Note that the configuration in this file must pay special attention to the format and indentation, otherwise the configuration may not take effect.