Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Dart, what is the difference between using 'new' keyword and directly calling constructor?

Tags:

flutter

dart

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?

like image 797
Arnav Gupta Avatar asked Apr 13 '18 00:04

Arnav Gupta


People also ask

What does the new keyword do in Dart?

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.

What is the use of this keyword while creating constructors in Dart?

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.

Do you need to use new in Dart?

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.

What is _() in Dart?

_(); 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.


1 Answers

In Dart 2.0 strong mode new and const are optional so both are the same.

like image 134
mythz Avatar answered Oct 21 '22 03:10

mythz