Flutter Learning 4: Flutter Development Basics (4) Package Management

Table of contents

0 Preface

1 package management

1.1 Introduction

1.2 Pub warehouse

1.3 Dependence on Pub repository

1.3.1 Find packages

 1.3.2 Add package

1.3.3 Download package

1.3.4 Importing packages

 1.3.5 Using packages

1.4 Other dependencies

1.4.1 Depend on local packages

1.4.2 Dependence on git repository

1.4.3 Uncommonly used dependency methods


0 Preface

This article is a study and summary of the preface to the second edition | "Flutter in Practice·Second Edition" (flutterchina.club) .

1 package management

1.1 Introduction

  • In software development, there are often some public libraries or SDKs that may be used by many projects. Therefore, extracting these codes into an independent module, and then directly integrating this module when a project needs to use it, can greatly improve the efficiency. Development efficiency.
  • Many programming languages ​​or development tools support this "module sharing" mechanism. For example, this independent module in the Java language will be packaged into a jar package, the aar package in Android, the npm package in web development, etc.
  • For ease of expression, we call this shareable independent module a "Package".
  • In actual development, an App often relies on many packages, and these packages usually have cross-dependencies, version dependencies, etc. It will be very troublesome for developers to manually manage the dependency 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, etc.
  •  Use configuration files (located in the project root directory) in Flutter pubspec.yamlto manage third-party dependency packages.

The default configuration file of the Flutter project is pubspec.yaml,the meaning of the fields in the configuration file as follows:

  • name: Application or package name.
  • description: 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 tool package 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 below dependencies .

注意:dependenciesThe difference between and dev_dependencies:

  • dependenciesThe dependent packages will be compiled as part of the App source code to generate the final installation package.
  • dev_dependenciesThe dependency packages are just some tool packages in the development stage, mainly used to help us improve development and testing efficiency, such as Flutter's automated testing package, etc.

Configuration file pubspec.yaml example:

name: 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

1.2 Pub warehouse

  • Pub ( https://pub.dev/ ) is Google’s official Dart Packages repository
  • Similar to the npm warehouse in node and jcenter in Android
  • You can find the packages and plug-ins we need on Pub
  • We can also publish our packages and plug-ins to Pub

1.3 Dependence on Pub repository

1.3.1 Find packages

First, find the english_words package on pub and determine its latest version number and whether it supports Flutter.

 1.3.2 Add package

  Add "english_words" to the project root pubspec.yaml文件内的dependencies list:

dependencies:
  flutter:
    sdk: flutter
  # 新添加的依赖
  english_words: ^4.0.0

1.3.3 Download package

While viewing pubspec.yaml in Android Studio's editor view, click  Pub get in the upper right corner  to download the package.

1.3.4 Importing packages

Import english_wordspackage. As you type, Android Studio automatically provides suggested options for library imports. After importing, this line of code will be displayed in gray, indicating that the imported library has not been used.

import 'package:english_words/english_words.dart';

 1.3.5 Using packages

Use english_wordsthe package to generate random strings.

class RandomWordsWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
   // 生成随机字符串
    final wordPair = WordPair.random();
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: Text(wordPair.toString()),
    );
  }
}



/*
将RandomWordsWidget 添加到 _MyHomePageState.build 的Column的子widget中。
*/
Column(
  mainAxisAlignment: MainAxisAlignment.center,
  children: <Widget>[
    ... //省略无关代码
    RandomWordsWidget(),
  ],
)

1.4 Other dependencies

1.4.1 Depend on local packages

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

Paths can be relative or absolute.

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

1.4.2 Dependence on git repository

You can also rely on packages stored in git repositories.

If the package is located in the root of the git repository, use the following syntax:

dependencies:
  pkg1:
    git:
      url: git://github.com/xxx/pkg1.git

If the package is not located in the root directory of the git repository, you can use the path parameter to specify a relative location, for example:

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

1.4.3 Uncommonly used dependency methods

There are some other less commonly used dependencies, check out: https://www.dartlang.org/tools/pub/dependencies

 

Guess you like

Origin blog.csdn.net/D_lunar/article/details/131420164