Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Dart have import alias?

I found myself written tedious code when importing files into dart files like the following:

import '../../constants.dart';

I'm wondering if there is any way to create an alias to specific folder like:

import '@shared/constants.dart';

Thanks, Javi.

like image 913
Javier Rodriguez Avatar asked Dec 12 '18 11:12

Javier Rodriguez


People also ask

What is the difference between as show and hide in an import statement?

import show : This is used to import part of the library, show only import one name of the library. import hide : This is another one which is the opposite of the show , hide import all names except the name specified in the hide .


1 Answers

Dart doesn't allow you to rename imported identifiers, but it allows you to specify an import prefix

import '../../constants.dart' as foo;

...

foo.ImportedClass foo = foo.ImportedClass();

It allows also to filter imported identifiers like

import '../../constants.dart' show foo hide bar;

See also

  • https://www.dartlang.org/guides/language/language-tour#libraries-and-visibility
  • What is the difference between "show" and "as" in an import statement?

Barrel files can also make importing easier like

lib/widgets/widgets.dart

export 'widget1.dart';
export 'widget2.dart';
export 'widget3.dart';
export 'widget4.dart';

lib/pages/page1.dart

import '../widgets/widgets.dart';

Widget build(BuildContext context) => Widget1();
like image 60
Günter Zöchbauer Avatar answered Oct 10 '22 05:10

Günter Zöchbauer