How flutter uses configuration file pubspec.yaml to manage third-party dependencies

Official documents

In software development, many times there are some public libraries or SDKs that may be used by many projects. Therefore, separate these codes Extracting an independent module, and then directly integrating this module when a project needs to be used, can greatly improve the development efficiency. Many programming languages ​​or development tools support this “module sharing” mechanism. For example, this independent module in the Java language will be labeled as a jar package, the aar package in Android, and the npm package in web development. For the convenience of presentation, we collectively refer to this sharable independent module as a “package” (Package).

In actual development, an APP often depends on many packages, and these packages usually have cross-dependencies, version dependencies, etc. It will be very troublesome if the developer manually manages the dependent packages in the application. Therefore, various development ecology or programming language officials usually provide some package management tools. For example, Android provides Gradle to manage dependencies, iOS uses Cocoapods or Carthage to manage dependencies, and Node uses npm. And in Flutter development, there is also its own package management tool. In this section, we mainly introduce how flutter uses the configuration file pubspec.yaml (located in the project root directory) to manage third-party dependency packages.

YAML is a file format that is intuitive, highly readable and easy to be read by humans. Compared with xml or Json, it has simple syntax and is very easy to parse, so YAML is often used in configuration files, Flutter It also uses yaml files as its configuration files. The default configuration file of the Flutter project is pubspec.yaml, let’s look at a simple example:

name< span class="token punctuation">: flutter_in_action description: First Flutter application. version: 1.0.0+1 dependencies: flutter: sdk: flutter cupertino_icons: ^0.1.2 dev_dependencies: flutter_test: sdk: flutter flutter: uses-material-design: true < /span>

Next, let’s explain the meaning of each field one by one:

< ul>

  • name: Application or package name.
  • description: The description and introduction of the application or package.
  • version: The version number of the application or package.
  • dependencies: Other packages or plug-ins that the application or package depends on.
  • dev_dependencies: The toolkit that the development environment depends on (not the package that the flutter application itself depends on).
  • flutter: flutter-related configuration options.
  • If our Flutter application itself depends on a certain package, we need to add the dependent package under dependencies, then we will demonstrate it through an example How to add, download and use third-party packages.

    Pub warehouse

    Pub (https://pub.dartlang.org/) is Google’s official Dart Packages warehouse, similar to the npm warehouse in node , Jcenter in android. We can find the packages and plugins we need on Pub, and we can also publish our packages and plugins to Pub. We will introduce how to publish our packages and plugins to Pub in later chapters.

    example

    Next, we implement a widget that displays a random string. There is an open source software package called “english_words”, which contains thousands of commonly used English words and some useful functions. We first find the english_words package on pub (as shown in Figure 2-5) to determine its latest version number and whether it supports Flutter.

    图2-5

    We see The latest version of the “english_words” package is 3.1.3 and supports flutter. Next:

    1. Add “english_words” (version 3.1.3) to the dependency list, as follows:

      dependencies: flutter: sdk: flutter cupertino_icons: ^0.1 .0 # Newly added dependencyenglish_words: ^3.1.3 < /span>
    2. Download Bag. When viewing pubspec.yaml in the editor view of Android Studio (Figure 2-6), click Packages get in the upper right corner.

      图2-6

      This will Dependent packages are installed into your project. We can see the following in the console:

      flutter packages get
      Running "flutter packages get" in flutter_in_action... Process finished with  exit code 0 

      We can also locate the current project directory in the console, and then manually run flutter packages get code> command to download the dependency package. In addition, you need to pay attention to the difference between dependencies and dev_dependencies. The dependency package of the former will participate in the compilation as part of the APP source code to generate the final installation package. The latter's dependency package is only used as some toolkits in the development stage, mainly used to help us improve the efficiency of development and testing, such as the automated test package of flutter.

    3. Introduce the english_words package.

      import 'package:english_words/english_words.dart' ; 

      When typing, Android Studio will automatically provide suggested options for library import. After importing, the line of code will be grayed out, indicating that the imported library has not been used yet.

    4. Use the english_words package to generate random strings.

      class RandomWordsWidget extends StatelessWidget {   span>

      We will add RandomWordsWidget to _MyHomePa In the sub widget of Column of geState.build.

      Column( mainAxisAlignment: MainAxisAlignment.center, children: >[ ... //omit Irrelevant codeRandomWordsWidget(), ], )   span>
    5. If the application is running, please use the hot reload button (?? icon) to update the running application. Each time you click hot reload or save a project, a different word pair is randomly selected in the running application. This is because the word pair is generated inside the build method. During each hot update, the build method will be executed, and the running effect is shown in Figure 2-7.

      图2-7

    6. Other dependence methods

      The above-mentioned dependence methods are dependent on the Pub repository. But we can also rely on local packages and git repositories.

      • Depending on a local package

        If we are developing a package locally and the package name is pkg1, we can rely on it in the following ways:

        dependencies: pkg1: path: ../../code/pkg1 

        The path can be relative or absolute.

      • Rely on Git: You can also rely on packages stored in the Git repository. If the package is located in the root directory of the repository, please use the following syntax

        dependencies: pkg1: git:  url: git://github.com/xxx/pkg1.git < /span>

        The above assumes that the package is located in the root directory of the Git repository. If this is not the case, you can use the path parameter to specify the relative position, for example:

        dependencies: package1: git: url: git://github.com/flutter/packages.git path: packages/package1 < /span>

      The dependency methods introduced above are commonly used in Flutter development, but there are some other dependency methods. Readers can view the complete content by themselves: https:/ /www.dartlang.org/tools/pub/dependencies.

    WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
    SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 2290 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

    Leave a Comment

    Your email address will not be published.