How to use this in a dart constructor with private variables

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;
  }

Leave a Comment

tech