Ben Ghaleb
Ben Ghaleb

Reputation: 483

What is the difference between these two packages importing ways in Dart language?

There are two ways to import packages in Dart programming language, I need to know what is the difference between them both, please? Also when to use the first way and when to use the second way?

First way:

import 'dart:io';

Second way

import 'dart:io' as io;

Upvotes: 0

Views: 208

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657376

as io specifies a prefix.

Without you can use members dart:io exports like

var f = File();

with prefix it would be

var f = io.File();

This is useful to disambiguate imports if names collide with declarations in your current library or another imported library.

Packages like path assume that they are imported with a prefix, because it exports many top-level functions with common names that without a prefix would clutter the scope.

Upvotes: 5

Related Questions