当前位置:网站首页>[flutter 1-14] dart language -- dart language reference, import

[flutter 1-14] dart language -- dart language reference, import

2020-12-08 13:45:04 osc_ eqcu0796

author | Vlad
source | Vlad ( official account :fulade_me)

library

import Keywords can help you create a modular and shareable code base , The code base doesn't just provide API And it also plays a role in packaging : The following line (_) The starting member is only visible in the code base .

Use the library

Use import To specify a namespace so that other libraries can access . For example, you can import the code base dart:html To use Dart Web Middle correlation API:

import 'dart:html';

import The only parameter of is used to specify the code base URI, about Dart Built in Library , Use dart:xxxxxx In the form of . And for other libraries , You can use a file system path or use package:xxxxxx In the form of .package:xxxxxx The specified library passes through the package manager ( such as pub Tools ) To provide :

import 'package:test/test.dart';

Specify the library prefix

If you import two codebases with conflicting identifiers , You can specify a prefix for one of them . For example if library1 and library2 There are Element class , So you can do this :

import 'package:lib1/lib1.dart';
import 'package:lib2/lib2.dart' as lib2;
//  Use  lib1  Of  Element  class .
Element element1 = Element();
//  Use  lib2  Of  Element  class .
lib2.Element element2 = lib2.Element();

Import part of Library

If you only want to use a part of the code base , You can selectively import the code base . for example :

//  Import only  lib1  Medium  foo.(Import only foo).
import 'package:lib1/lib1.dart' show foo;
//  Import  lib2  In addition to  foo  Everything else .
import 'package:lib2/lib2.dart' hide foo;

Delay loading the library

Delay loading ( Also known as lazy loading ) Allow the application to load the code base when needed , Here are some scenarios where delayed loading may be used :

  • In order to reduce application initialization time .

  • Handle A/B test , Such as testing different implementations of various algorithms .

  • Load features that are rarely used , For example, optional screens and dialog boxes .

Use deferred as Keyword to identify the code base that needs delay loading :

import 'package:greetings/hello.dart' deferred as hello;

When you actually need to use it in the library API Call... First loadLibrary Function loading Library :

Future greet() async {
  await hello.loadLibrary();
  hello.printGreeting();
}

In the previous code , Use await Keyword to pause code execution until the library load is complete . More about async and await Please refer to asynchronous support .
loadLibrary It doesn't matter if the function can be called many times , The code base will only be loaded once .
Keep in mind the following when you need to use the load delay :

  • Constants in the code base that are delayed to load are imported only when the code base is loaded , It will not be imported when it is not loaded .
  • The type in the delayed load library cannot be used when importing files . If you need to use type , Consider transferring the interface type to another library, and then having both libraries import the interface library separately .
  • Dart Will implicitly loadLibrary Method is imported into the use of deferred as Namespace In the class .loadLibrary The function returns a Future.

 official account

版权声明
本文为[osc_ eqcu0796]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/20201208134412323n.html