Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export two library which have same class name

Tags:

dart

I met an error when exporting two libraries. And these libraries have exactly same class name.

File: A.dart

library chrome.A; class MyClass { ... } 

File: B.dart

library chrome.B; class MyClass { .. } 

File: C.dart

library chrome_app; export 'A.dart'; export 'B.dart';  // HERE!! error message for the element 'MyClass' which is defined in the libraries 'A.dart' and 'B.dart' 

Is this the intended result?

I think A.dart and B.dart has their own namespace so that there should be no error.

like image 492
Sungguk Lim Avatar asked Mar 19 '14 01:03

Sungguk Lim


People also ask

How do I import two libraries with the same name?

To import two classes with the same name, use the as keyword to rename one or both of the imports, e.g. import {Employee as Employee2} from './another-file-2. js'; . The as keyword allows us to change the identifying name of the import.

Can two classes have same name?

Yes, you can have two classes with the same name in multiple packages. However, you can't import both classes in the same file using two import statements. You'll have to fully qualify one of the class names if you really need to reference both of them.

Can a class in Java be named the same as another class when?

Yes, It is allowed to define a method with the same name as that of a class. There is no compile-time or runtime error will occur. But this is not recommended as per coding standards in Java. Normally the constructor name and class name always the same in Java.

What is export in Dart?

Exports the current Flutter internationalisation files to a singl CSV so that all translations can be worked on in on document. Once translation is complete, you can then use CreateCommand to generate new arb files to use in your Flutter project.


1 Answers

A library name isn't a namespace. Dart doesn't have namespaces.
What you can do in Dart is to specify a prefix for an import.

You have to import those libraries separately if you want to use them in the same library instead of just one import with import 'C.dart;'

import 'A.dart' as a; import 'B.dart' as b;  var m = a.MyClass(); var n = b.MyClass(); 

If you just want to avoid the conflict and don't need both classes exported you can.

library chrome_app;  export 'A.dart'; export 'B.dart' hide MyClass; // or export 'B.dart' show MyOtherClass, AnotherOne; // define explicitly which classes to export and omit MyClass 
like image 60
Günter Zöchbauer Avatar answered Oct 23 '22 17:10

Günter Zöchbauer