0

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;
Ben Ghaleb
  • 463
  • 4
  • 9
  • 20

1 Answers1

5

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.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567