Dart 库与导入


Flutter import of part
导入文件import路径代表的意思
文件导入的顺序
import ‘dart:xxx’; 引入Dart标准库
import ‘xxx/xxx.dart’;引入绝对路径的Dart文件
import ‘package:xxx/xxx.dart’; 引入Pub仓库pub.dev(或者pub.flutter-io.cn)中的第三方库
import ‘package:project/xxx/xxx.dart’;引入自定义的dart文件
import ‘xxx’ show compute1,compute2 只导入compute1,compute2
import ‘xxx’ hide compute3 除了compute都引入
import ‘xxx’ as compute4 将库重命名,当有名字冲突时
library compute5; 定义库名称
part of compute6; 表示文件属于某个库

文件导入顺序从上到下依次
dart sdk 内的库
flutter内的库
第三方库
自己的库文件
相对路径引用
e.g.
import ‘dart:io’;
import ‘package:material/material.dart’;
import ‘package:dio/dio.dart’;
import ‘package:project/common/uitls.dart’;
import ‘xxx/xxx/xxx/xxx.dart’;

Dart用import导入一个库 后面跟字符串Uri指定引用的库
// 指定dart:前缀 表示导入标准库 如dart:io
import 'dart:math';
// 也可以用相对路径或绝对路径来引用dart文件
import 'lib/student/student.dart';
// 指定package:前缀 表示导入包管理系统中的库
import 'package:utils/utils.dart';
导入库时 可以使用as关键字来给库起别名
避免命名空间冲突
import 'package:lib1/lib1.dart';
import 'package:lib2/lib2.dart' as lib2;
// 使用lib1中的Element
Element element1 = new Element();
// 使用lib2中的Element
lib2.Element element2 = new lib2.Element();
使用show和hide关键字控制库中成员的可见性
// 仅导入foo 屏蔽库中其他成员
import 'package:lib1/lib1.dart' show foo;
// 屏蔽foo 库中其他成员都可见
import 'package:lib2/lib2.dart' hide foo;
为了减少 APP 的启动时间
加载很少使用的功能
可以延迟导入库
使用 deferred as 关键字延迟导入
import 'package:deferred/hello.dart' deferred as hello;
// 当需要使用时 再通过库标识符调用 loadLibrary函数加载
hello.loadLibrary();