What does colon mean when importing core libraries in Dart?

What does colon mean when importing core libraries in Dart?

import 'dart:math';

I haven't find any resources that tells what does that mean.

Upvotes: 0

Views: 125

Answers (3)

lrn
lrn

Reputation: 71828

Dart imports are specified using a URI, or a relative URI reference.

URIs have a scheme, which come before the first :. The scheme defines how to interpret the part that comes after the :.

The import import 'dart:html'; uses a URI with the scheme dart and the scheme-specific resource identifier html. The dart scheme is used to access platform libraries, so dart:html is the platform HTML library.

An import like import 'package:test/test.dart'; uses the package scheme, which is another Dart-specific scheme. The scheme is followed by a package name (test) and a path, /test.dart, which refers to a file inside that package.

An import like import 'file:///home/myself/src/dart/mypkg/bin/main.dart;used thefile` scheme to point to a file on the local machine.

An import like import 'src/helper.dart'; uses a relative URI reference, which has no scheme, but which is resolved against the URI of the containing library to create a complete URI.

In the first three cases, the colon is the URI scheme separator. The schemes dart and package are specific to Dart tools, and file is a general URI scheme.

Upvotes: 3

From the documentation (emphasis mine):

Using libraries

Use 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:

import 'dart:html';

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:

import 'package:test/test.dart';

Note: URI stands for uniform resource identifier. URLs (uniform resource locators) are a common kind of URI.

Upvotes: 0

Gicu Aftene
Gicu Aftene

Reputation: 532

You should use colon when you try to import other packages modules... You can omit it when the module that you are importing is in the same package.

import '<package>:<module>'; // normal import
/* 
  if the module is the in the same package of the file where i'm importing it 
*/
import '<module>';

Upvotes: 0

Related Questions