12

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?

0xcaff
  • 13,085
  • 5
  • 47
  • 55
st_clair_clarke
  • 5,453
  • 13
  • 49
  • 75

3 Answers3

12

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.

silverbeak
  • 312
  • 3
  • 12
Pixel Elephant
  • 20,649
  • 9
  • 66
  • 83
  • Glad to help :) Please mark the answer as solved if the issue is resolved. – Pixel Elephant May 22 '13 at 16:03
  • Thanks very much. But this problem only arose after the my update to the current Dart Editor. Everything was quite fine before. The other thing is the second and third import above are NOT libraries, they are dart source file I created outside of the lib directory. Do you still think I should place all my files in a particular library. I have tried doing that, but although I see the library names in the generated package, whenever I attempt to reference them in the standard manner, the code always fails. Thanks for your help. – st_clair_clarke May 22 '13 at 16:04
  • It is up to you to decide whether they are logically their own libraries or part of your main library (would you ever want to reuse the code in a different library?). If they are not their own library there is no need to import anything. – Pixel Elephant May 22 '13 at 16:14
  • Previous to my update, everything works. I wonder if I am doing something incorrectly. Even when two .dart files are present in the same directory, I had to have an import statement for the file to be referenced without an error. Is there another mechanism to do this? Thanks – st_clair_clarke May 22 '13 at 16:21
  • I'm not sure about the editor update changing things, but in order to use other files within the same library you have to either use "part" to specify that the file is part of the library or "import" to use a different library. Sorry if it is not clear, you can read the link that I provided for more info. – Pixel Elephant May 22 '13 at 16:31
  • You have provided a great help Pixel Elephant. From the article you mentioned I am now able to import from my local library! I found out that I cannot do import "package:....", but rather import "./packages/...", small difference but a mandatory one. Thanks. – st_clair_clarke May 22 '13 at 16:42
  • 2
    The latest update contained updates/fixes to the new analyzer which probably was not checking for library naming clashes. Whenever you use 'import' it importing a library. This library _should_ have a declared library (exception being the file with your main() function). See this question regarding how you can combine two files with either import or part: http://stackoverflow.com/questions/12951989/how-to-reference-another-file-in-dart – Matt B May 22 '13 at 17:09
  • Thanks MattB, but the cryptic warning by Dart's editor is of not much help! The name of the files being imported are quite different! Could it be that the clash of names is in similar imported files/libraries? It would be helpful if the Editor would be more specific. I have tried everything to find the reason for the errors without much success. – st_clair_clarke May 24 '13 at 01:22
3

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';

Alexandre Ardhuin
  • 71,959
  • 15
  • 151
  • 132
3

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.

Hoylen
  • 16,076
  • 5
  • 30
  • 16