How to create private variables in Dart?

I want to create a personal variable, but I can’t.

Here is my code:

void main() { var b = new B(); b.testB(); } class A { int _private = 0; testA() { print('int value: $_private'); _private = 5; } } class B extends A { String _private; testB() { _private = 'Hello'; print('String value: $_private'); testA(); print('String value: $_private'); } } 

When I run this code, I get the following result:

 String value: Hello int value: Hello Breaking on exception: type 'int' is not a subtype of type 'String' of 'value'. 

Also, I do not get any errors or warnings when editing this source code.

How can I create a personal variable in Dart?

+17
dart
source share
3 answers

From the Dart documentation:

Unlike Java, Dart does not have public, secure, or private keywords. If the identifier begins with the underscore _ , its private access to its library.

Libraries not only provide APIs, but are also part of privacy: identifiers starting with the underscore _ are visible only inside the library.

+35
source share

Privacy in Darth exists in the library, not at the class level.

If you must put class A in a separate library file (for example, other.dart ), for example:

 library other; class A { int _private = 0; testA() { print('int value: $_private'); // 0 _private = 5; print('int value: $_private'); // 5 } } 

and then import it into the main application, for example:

 import 'other.dart'; void main() { var b = new B(); b.testB(); } class B extends A { String _private; testB() { _private = 'Hello'; print('String value: $_private'); // Hello testA(); print('String value: $_private'); // Hello } } 

You get the expected result:

 String value: Hello int value: 0 int value: 5 String value: Hello 
+12
source share

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:

  //This should be in a separate library from main() for the reason stated in the main method below. class MyClass { //Library private variable int _val = 0; int get val => _val; set val(int v) => _val = (v < 0) ? _val : v; MyClass.fromVal(int val) : _val = val; } void main() { MyClass mc = MyClass.fromVal(1); mc.val = -1; print(mc.val); //1 //main() MUST BE IN A SEPARATE LIBRARY TO //PREVENT MODIFYING THE BACKING FIELDS LIKE: mc._val = 6; print(mc.val); //6 } 

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.

+2
source share

All Articles