Use of flutter series 4-dart library

Custom library

import ’lib/xxx.dart'

System built-in library

import ‘dart:math'
import 'dart:io'

Pub package management system library

pub warehouse address:
https://pub.dev/packages
https://pub.flutter-io.cn/packages

  1. Create a new pubspec.yaml file in the root directory of your own project
  2. Configure the name, description, dependencies and other information in the pubspec.yaml file
  3. Run pub get to download the package to the local
  4. Introduce the library import 'package: http / http.dart' as http in the project

For example, if we want to find http dependencies from pub, we directly open the pub warehouse address:
Insert picture description here
directly search to find the version corresponding to http, and add the http dependency to the pubspec.yaml file

name: xxx
description: A flutter module
dependencies:
  http: ^0.12.0+4

After the pub get installation is completed, then follow the example in the warehouse and refer to your own code:

Insert picture description here

Package conflict resolution

If the same class is defined in the two dart files introduced, then we use as to resolve the conflict

import 'lib/Person1.dart';
import 'lib/Person2.dart' as per;

main(List<String> args) {
  Person person = new Person();
  per.Person person2 = new per.Person();
}

Both Person1 and Person2 define the class of Person. If you do not apply as at the same time, you will get an error when creating the Person class. After using as, you will clearly specify the Person class in the dart file when you create it. Too.

Import some methods

Use the show keyword to refer to a method in a dart file

import  'lib/MyMath.dart' show getName
Published 159 original articles · 22 praises · 90,000+ views

Guess you like

Origin blog.csdn.net/ytuglt/article/details/105099901