38

When I try to create a constructor in dart like Student(this._name) it doesn't work with private variables.

I have already tried using setters but it doesn't work either.

    class Student{

    var _id;
    var _name;

    Student(this.id, this.name);

    void set id(int id) => _id = id;
    void set name(String name) => _name = name;

    }
3
  • 1
    Sure, it does. It does not only if you make them optional named parameters (Student({this._id, this._name}); would not be allowed) Jan 24, 2019 at 18:30
  • Ok, so in a case with optional named parameters I would have to set the variables in the constructor body? Jan 24, 2019 at 18:37
  • See my answer. Some hints. User proper types for fields instead of var. var is fine for local variables that are initialized when declared. Don't use public getters/setters for private fields if they don't contain additional logic. You can replace fields by getters/setters or vice versa at any time without breaking users of your code. Jan 24, 2019 at 18:48

4 Answers 4

50

This is not supported because it would expose private implementation to the outside.

If you'd rename var _id; to var _userId; you would break code that uses your class just by renaming a private field.
See instead the comment below my answer.

  class Student{

    var _id;
    var _name;

    Student({this._id, this._name}); // error

    void set id(int id) => _id = id;
    void set name(String name) => _name = name;
  }

The alternative

  class Student{

    var _id;
    var _name;

    Student({int id, String name}) : _id = id, _name = name;

    void set id(int id) => _id = id;
    void set name(String name) => _name = name;
  }
3
  • 5
    The reason privately named parameters are not allowed is that you can't call with them from a different library. If you declare foo({int _x}) {...} then no other library can provide that argument because foo(_x: 42) is a different name in a different library. For instance methods, no other library would be able to override the methods. So, in general, named parameters are not allowed to have private names. (It's not because changing the name would break anyone, because noone could possibly be using that name at all).
    – lrn
    Jan 25, 2019 at 9:43
  • 1
    Are there any comments on whether this solution is idiomatic Dart or not? It seems that hiding members (e.g., dependencies) from consumers is a concern that is intentionally avoided by the language choices in Dart Apr 11, 2020 at 2:56
  • 1
    Wish Dart would just assume that this.id should set a private _id variable. I understand this would make code "less flexible" but I think the benefit of keeping it the way it is right now is very limited Jan 22 at 20:43
8

You can use this notation

class Student {
  String _id;
  String _name;

  Student({required String id, required String name})
      : _id = id,
        _name = name;
}
4

Some of you maybe struggle if the class was inheritance, you just need add coma (,) after initialize your private.

Example

class Animal {
  String _name;
  int _age;
}

class Dog extends Animal {
  String _race;
  Dog(String name, int age, {String? race}) : _race = race ?? "Wild", super(name, age);
}

Hope this code can help you.

-1

This notation is not valid because the variable is not private and its elements are just as accessible again.

DartLang says: AVOID wrapping fields in getters and setters just to be "safe".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.