I see I am allowed to use
argument: MyClassName(a,b)
as well as
argument: new MyClassName(a,b)
I wanted to understand is new
optional in Dart? Or these two return totally different things?
The new keyword is used to create an instance of a class. However, Dart 2 makes the new keyword optional. Calling a class will always return a new instance of that class.
The this keyword is used to point the current class object. It can be used to refer to the present class variables. We can instantiate or invoke the current class constructor using this keyword. We can pass this keyword as a parameter in the constructor call.
In Dart 2, the new keyword is optional. If you want, you can continue to ignore it, and this will not have effect on your program.
_(); is a named constructor (another examples might be the copy constructor on some objects in the Flutter framework: ThemeData. copy(...); ). In dart, if the leading character is an underscore, then the function/constructor is private to the library.
In Dart 2.0 strong mode new and const are optional so both are the same.
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