I had the same problem, but someone suggested the following solution that works for me. I will demonstrate with a brief example:
library view; abstract class View { factory View({HtmlElement host}) = ViewImpl; void activate() { } } abstract class ViewImpl implements View { ViewImpl({this.host}); @override void enable() { host.classes.remove('disabled'); } }
In another library:
library button; // notice, this is another library abstract class Button extends View { factory Button({String title, HtmlElement host}) = ButtonImpl; // The magic happens here! } class ButtonImpl extends ViewImpl implements Button { String title; // voila, a protected property ButtonImpl({this.title, HtmlElement host}) : super(host: host); }
You can use it as follows:
final button = new Button(title: "Cancel", host: element); button.title // => cannot access!
In addition, you can access a protected field, for example. a unit test:
// Instantiate the *Impl class to side-step the protection. final button = new ButtonImpl(title: "Cancel", host: element); expect(button.title, equals("Cancel"); // => it works!
In short, you are hiding your "protected" methods in Impl classes. You can freely expand the implementation in different libraries.
Hope this helps.
source share