Export two libraries that have the same class name

I found an error while exporting two libraries. and these libraries have exactly the 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 is the element 'MyClass' is defined in the libraries 'A.dart' and 'B.dart'

Is this the intended result?

I think that A.dart and B.dart have their own namespace so that there are no errors.

+4
source share
1 answer

The name of the library is not a namespace. Dart has no namespaces.
What you can do in Dart is to specify a prefix for the import.

You need to import these libraries separately if you want to use them in the same library instead of a single import with import 'C.dart;'

import 'A.dart' as a;
import 'B.dart' as b;

var m = new a.MyClass();
var n = new b.MyClass();

If you just want to avoid conflict and don't want to export both classes, 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
+7

Source: https://habr.com/ru/post/1532392/


All Articles