Is there a way to set a constructor optional param? I mean something like:
User.fromData(this._name, this._email, this._token, this._refreshToken, this._createdAt, this._expiresAt, this._isValid, {this.id});
It indicates that
Named option parameters can't start with an underscore.
But I need this field as private, so, I'm lost now.
The definition of a method, constructor, indexer, or delegate can specify its parameters are required or optional. Any call must provide arguments for all required parameters, but can omit arguments for optional parameters. Each optional parameter has a default value as part of its definition.
You can't have optional arguments that default to a certain value in Java. The nearest thing to what you are talking about is java varargs whereby you can pass an arbitrary number of arguments (of the same type) to a method.
Like any object-oriented language, you can have an optional argument in a constructor in TypeScript also. The ? keyword is used in the argument to make it optional for the constructor. All the optional arguments of a constructor should be placed after all the mandatory arguments only in a constructor.
Java For TestersThis constructor is implemented by default by the Java compiler if there is no explicit constructor implemented by the user for the class. If you observe the following example, we are not providing any constructor to it.
This is a more general answer for future viewers.
Wrap the optional parameter with [ ]
square brackets.
class User { String name; int age; String home; User(this.name, this.age, [this.home = 'Earth']); } User user1 = User('Bob', 34); User user2 = User('Bob', 34, 'Mars');
Optional parameters need to be nullable if you don't provide a default value:
class User { String name; int age; String? home; // <-- Nullable User(this.name, this.age, [this.home]); }
Wrap the optional parameter with { }
curly braces.
class User { String name; int age; String home; User(this.name, this.age, {this.home = 'Earth'}); } User user1 = User('Bob', 34); User user2 = User('Bob', 34, home: 'Mars');
The default for home
is "Earth", but like before, if you don't provide a default then you need to change String home
to String? home
.
If you need private fields then you can use []
square brackets:
class User { int? _id; User([this._id]); } User user = User(3);
or do as the accepted answer says and use an initializer list:
class User { int? _id; User({int? id}) : _id = id; } User user = User(id: 3);
Named parameters are optional by default, but if you want to make them required, then you can use the required
keyword:
class User { final String name; final int age; final String home; User({ required this.name, required this.age, this.home = 'Earth', }); } User user1 = User(name: 'Bob', age: 34); User user2 = User(name: 'Bob', age: 34, home: 'Mars');
You need to use a simple parameter and initialize your private field in initializer list.
class User { final String _id; final String _name; User.fromData(this._name, {required String id}) : _id = id; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With