library directives can help you create a modular and shareable code base. Libraries not only provide APIs, but are a unit of privacy: identifiers that start with an underscore (
_) are visible only inside the library. Every Dart app is a library, even if it doesn’t use a
Libraries can be distributed using packages.
import to specify how a namespace from one library is used in the scope of another library.
For example, Dart web apps generally use the dart:html library, which they can import like this:
The only required argument to
import is a URI specifying the library. For built-in libraries, the URI has the special
dart: scheme. For other libraries, you can use a file system path or the
package: scheme. The
package: scheme specifies libraries provided by a package manager such as the pub tool. For example:
Specifying a library prefix
If you import two libraries that have conflicting identifiers, then you can specify a prefix for one or both libraries. For example, if library1 and library2 both have an Element class, then you might have code like this:
import 'package:lib1/lib1.dart'; import 'package:lib2/lib2.dart' as lib2; // Uses Element from lib1. Element element1 = Element(); // Uses Element from lib2. lib2.Element element2 = lib2.Element();
© Copyright By Larnr Education. 2021.
Developed By Shibaji Debnath.