Sungguk Lim
Sungguk Lim

Reputation: 6228

Export two library which have same class name

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.

Upvotes: 41

Views: 17026

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657957

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

Upvotes: 76

Related Questions