flutter09 - Package Manager

Flutter09 - Package Manager

A APP in the actual development tend to rely on a lot of packages, but these packages usually have cross dependencies, version dependence, if by the developer to manually manage application dependencies will be very troublesome. Therefore, the development of a variety of ecological or programming languages ​​usually provide some official package management tools:

  • Android provides Gradle to manage dependencies
  • iOS with Cocoapods or manage dependency Carthage
  • Node like by npm
  • flutter how to use the configuration filepubspec.yaml
name: first_flutter_app # 应用或包名
description: A new Flutter application. # 应用或包的描述/简介

version: 1.0.0+1 # 应用或包的版本号

environment:  # sdk环境
  sdk: ">=2.1.0 <3.0.0"

dependencies: # 应用或包依赖的其他包或插件
  flutter:
    sdk: flutter

 cupertino_icons: ^0.1.2

dev_dependencies: # 开发环境依赖的工具包
  flutter_test:
    sdk: flutter

flutter: # flutter相关的配置选项
  uses-material-design: true

The meaning of each field:

  • name: Application or package name.
  • description: Application or package descriptions, profiles.
  • version: The version number of the application or package.
  • dependencies: Other packages or package or plug-in application dependent.
  • dev_dependencies: Development environment dependent Kit (rather than the application itself dependent flutter package).
  • flutter: Flutter related configuration options.

Download and use third-party libraries

Pub (https://pub.dev/) is Google's official Dart Packages warehouse, warehouse node in a similar npm, android in jcenter.

Search third-party libraries that you need

Add a new dependency

cupertino_icons: ^0.1.2

# 新添加的依赖
english_words: ^3.1.3

Then click Packages get

We can see the following in console

/Users/a/flutter/bin/flutter --no-color packages get
Running "flutter pub get" in first_flutter_app...                   3.4s
Process finished with exit code 0

We can also console, navigate to the current project directory, and then manually run the flutter packages getcommand to download dependencies.

dependenciesAnd dev_dependenciesthe difference between:

The former dependencies as a part of the source code to compile participation of APP to generate the final installation package. The latter depends on the package just as some of the stages of development tools package, mainly used to help us improve the development, testing efficiency, such as flutter test automation packages, etc.

Using third-party libraries

import 'package:english_words/english_words.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final wordStr = WordPair.random(); //使用第三方库生成一个随机单词

    return MaterialApp(
      title: "使用第三方库生成一个随机单词",
      home: Scaffold(
        appBar: AppBar(
          title: Text("使用第三方库生成一个随机单词"),
        ),
        body: Center(
          child: Column(
            children: <Widget>[
              Text(
                "$wordStr",
              ),
            ],
          ),
        ),
      ),
    );
  }
}

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-kqtiVe7i-1584404599137) (https://s1.ax1x.com/2020/03/17/8trfyT.png )]

Other dependent manner

  • Dependent on local package

    If we are to develop a local package, the package called pkg1, we can rely on the following way:

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

    The path can be relative, also it can be absolute.

  • Dependence Git: You can also rely on packet stored in the Git repository. If the package repository located in the root directory, use the following syntax

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

    Given that the package Git repository at the root of the above. If this is not the case, the path parameter can be used to specify the relative position, for example:

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

But there are other dependent manner, complete readers to see: https: //www.dartlang.org/tools/pub/dependencies

Published 49 original articles · won praise 6 · views 80000 +

Guess you like

Origin blog.csdn.net/zlhyy666666/article/details/104913770