Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning that 'imported libraries have the same name when they DO NOT"

Tags:

dart

I have the following import statements in a class

import 'package:dart_web_toolkit/ui.dart';

import '../../util/flex_table_builder.dart' as ftBldr;

import '../factors_list_view.dart';


class MediatingFactorsView extends Composite
{
 //... 
}

However, the last import statment is flagged with a warning:

The different imported libraries 'flex_table_builder.dart' and 'factors_list_view.dart' should not have the same name

The names are quite different and I see this being repeated throughout my code after I updated to the latest Dart Editor. Is this a bug?

like image 758
st_clair_clarke Avatar asked May 22 '13 15:05

st_clair_clarke


3 Answers

The warnings correctly indicate there is a problem.

The correct solution depends on if the Dart files are conceptually separate from each other or are related.

Different libraries

If they are conceptually separate, then they should be defined as belong to different libraries with different library names.

The import command is used to reference a compilation unit from a different library, not a compilation unit that belongs to the same library. It expects every library to have a unique library name.

If a Dart file is treated as a library (i.e. is the subject of an import statement) and is not explicitly named then its implicit name is the empty string. In this case, both files are treated as libraries, and it is complaining about there being two libraries with the same name (of an empty string).

For this to work as separate libraries, given them different names:

Top file:

import 'foo.dart';
import 'bar.dart';

foo.dart:

library foo;

bar.dart:

library bar;

Part of the same library

But if they related they should not be referenced as libraries. That is, do not use the import command. Use the part command, which treats the Dart file as compilation unit that belongs to the same library.

Top file:

library baz;
part 'foo.dart';
part 'bar.dart';

foo.dart:

part of baz;

bar.dart:

part of baz;

Only the top library file can contain the part statements. You do not need (and cannot have) part statements inside the other files, even if they reference definitions from each other. If there are multiple files, just list them all in the top library file (in any order).

Although the implicit name for a library is the empty string, there is no way to use that when there are multiple parts involved: so even if these files are never going to be imported as a library, you will still need to explicitly assign it a library name.

Always remember: import and part statements are very different from the #include macro in the C preprocessor.

like image 193
Hoylen Avatar answered Sep 24 '22 13:09

Hoylen


Make sure that you are assigning library names to each file. The first line of the file should be:

library foo;

You should use different names for each library that you use. The library name is specified by the library directive, but anonymous libraries are treated as having the same name, which is where the warning comes from.

It is a warning in the Dart specification to import two libraries with the same name.

You can read more about how to use libraries here in the language tour.

like image 20
Pixel Elephant Avatar answered Sep 25 '22 13:09

Pixel Elephant


You could follow the Package layout conventions.

For the error :

The different imported libraries onset_view.dart and duration_view.dart should not have the same name

you should define distinct library names in each imported dart file.

One additionnal note, you should use import 'package:epimss/shared.dart'; instead of import 'packages/epimss/shared.dart'; and import 'site_view.dart'; instead of import './site_view.dart';

like image 44
Alexandre Ardhuin Avatar answered Sep 22 '22 13:09

Alexandre Ardhuin