30

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.

Javier Rodriguez
  • 545
  • 1
  • 4
  • 11
  • It's not an aliases, but in Flutter there is way to make one import, which includes some different imports. Maybe it can help to shortened your imports – Andrii Turkovskyi Dec 12 '18 at 11:24

2 Answers2

51

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

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();
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
6

No. Dart do not have import alias.

But you have absolute imports which makes up for it:

import 'package:my_lib/shared/constants.dart

This will import the file /lib/shared/constants.dart

Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432