The main answer at the moment, of course, is correct.
I will try to delve into this answer.
I will answer the question, but give the following: this is not exactly how Dart is intended to be written, in part because private library members make it easy to define operators like == . (The private variables of the second object could not be seen for comparison.)
Now that we have this, I will start by showing you how it should be done (a private library instead of a private class), and then showing how to make the class variable private if you still really want to. Like this.
If one class does not have business variables visible in another class, you can ask yourself if they really belong to the same library:
That should be good. However, if you really want private class data:
Although you are technically not allowed to create private variables, you can emulate them using the following closing technique.
(HOWEVER, YOU SHOULD CAREFULLY THINK if you really need it and if there is a better, more dart-like way to do what you are trying to achieve!)
//A "workaround" that you should THINK TWICE before using because: //1. The syntax is verbose. //2. Both closure variables and any methods needing to access // the closure variables must be defined inside a base constructor. //3. Those methods require typedefs to ensure correct signatures. typedef int IntGetter(); typedef void IntSetter(int value); class MyClass { IntGetter getVal; IntSetter setVal; MyClass.base() { //Closure variable int _val = 0; //Methods defined within constructor closure getVal = ()=>_val; setVal = (int v) => _val = (v < 0) ? _val : v; } factory MyClass.fromVal(int val) { MyClass result = MyClass.base(); result.setVal(val); return result; } } void main() { MyClass mc = MyClass.fromVal(1); mc.setVal(-1); //Fails print(mc.getVal()); //On the upside, you can't access _val //mc._val = 6; //Doesn't compile. }
So yes. Just be careful and try to follow the language recommendations, and you will be fine.
EDIT
Obviously, there is a new typedef syntax that is preferred by Dart 2. If you use Dart 2, you should use it. Or, even better, use the built-in function types.
If you use the second, it will be less verbose, but other problems will remain.