Dependency management

In native development, Android uses Gradle to manage dependencies and iOS uses Cocoapods or Carthage to manage dependencies. Flutter uses the configuration file pubspec.yaml (located at the project root) to manage third-party dependencies on Package.

YAML is an intuitive, readable, and human-readable file format. Compared with XML or Json, it has a simple syntax and is very easy to parse. Therefore, YAML is often used as a configuration file. The default configuration file for the Flutter project is pubspec.yaml.

In particular, in daily development, yamL files are edited to pay attention to the level of space, sometimes it is clearly copied, but because of an extra space indented, the error is reported

Depend on the source

1. The Pub warehouse

Pub is Google’s official Dart Packages repository, similar to the NPM repository in Node and JCenter in Android. You can find the Packages and plug-ins you need and publish them to Pub. Eg:

dependencies:
  # network packetHTTP: 0.12.1Copy the code

2. The git repository

  1. At the git repository root
Dependencies: two packages pkg1: git: url: git://github.com/xxx/pkg1.git / / in the git repository root directoryCopy the code
  1. Not at the root of your Git repository
Dependencies: package1: git: url: git://github.com/flutter/packages.git path: packages/package1 / / isn't git repository root directoryCopy the code

3. The local package

dependencies: pkg1: path: .. /.. /code/pkg1 // Local pathCopy the code

Install and use

1. Add dependencies

Open the pubspec.yaml file and add HTTP under dependencies(proje-pubspec.yaml-dependencies):

dependencies:
  # network packetHTTP: 0.12.1Copy the code

2. Install dependencies

  1. Method one:In terminal: Runningflutter packages get
  2. Method 2:In IntelliJ/AS IDE: Clickpubspec.yamlTop of filePackages Get

3. Import import

Import the corresponding package in the file, where as is equivalent to alias to prevent method name conflict import'package:http/http.dart' as http;
Copy the code

4. Upgrade the dependency package

If you modify the pubspec.yaml file, or just want to update the packages that your application depends on (excluding the Flutter SDK), use the following command:

Yaml gets the latest version of all dependencies listed in the pubspec.yaml file. After getting the version number, modify pubspec.yaml file. Flutter packages can be upgraded by using flutter packages getCopy the code

Version control

dependencies:
  # specify versionXxPlugin: 0.12.1# specify a minimum and maximum version number
  xxPlugin: '> = 0.1.2 < 0.2.0'
  
  # limited version '^0.1.2' // large version unchanged interval equivalent to '>=0.1.2 <0.2.0'XxPlugin: ^ 0.1.2# Any version
  xxPlugin: any
  
  #  >3.0.9
  xxPlugin: '> 3.0.9'// Note the quotation marksCopy the code

For ^ see caret syntax

Environmental control

// This is the production environment where the Package is packaged together with the Package# The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for iOS style icons.Cupertino_icons: ^ 0.1.3xxplugin: ^3.0.9 Flutter xxPlugin: ^ 3.0.9Copy the code

This section refers to Flutter development – Flutter pub package management