132

In my Dart based application I just noticed that I can omit the new keyword and everything works perfectly fine.

Instead of final widget = new Widget(); I can also use final widget = Widget();.

Does this have any effect in code?

creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402

3 Answers3

167

No, it does not. With Dart 2 (click for the announcement with more information) the new and also const keywords were made optional.

This means that new Widget() does the exact same as Widget() on its own.


The const keyword can, however, change a value that would not be a const implicitly to a const.
So you will have to explicitly specify const when needed.

creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
  • 6
    The Dart team had to retract a bit for now and there are some situations where `new` or `const` are still required (I don't remember examples or rules). They might make another attempt after Dart 2 to make it optional everywhere. – Günter Zöchbauer Apr 29 '18 at 21:30
  • 1
    There are no places where `new` is required. If you omit `new` from a correct program, it will always keep working the same way. If you remove `const` from a working program, then in most cases where you would *have* to write `const` for the program to be valid, it will still be valid. Any time there is already a `const` surrounding the expression, you can omit the nested const. – lrn Oct 18 '18 at 09:08
25

In Dart 2, if you invoke a constructor like a function, without a new or const in front, then it is equivalent to using new. If you want a const invocation, then you should put const in front.

Inside a const expression, you don't need to write const again, and in some contexts that require const expressions (like switch case expressions and initializers of const variables), you don't even need the outer const.

So you don't ever need to write new.

Dart language team wants to allow expressions where you can insert either new or const and still have the invocation be correct (that is, a const constructor with constant arguments) to default to inserting const instead of new, hopefully in an early update to Dart 2. For that reason, I recommend writing new it in front of Object(), or any other const constructor where you need the object to be a new instance. That's a very rare case, usually you don't care about the identity of your immutable object (which is why inserting const is considered a good idea). (That plan didn't pan out, so you can ignore this.)

lrn
  • 64,680
  • 7
  • 105
  • 121
  • The `new` and `const` keywords are different. They have a different effect in the memory usage in compilation and runtime. – Frank Moreno Jan 11 '21 at 02:09
6

The new keyword was made optional in Dart 2. As of now, calling a class will always return a new instance of that class. As per my recommendation, you can use it (NOT MANDATORY) outside of a Layout definition, but omit in inside Layouts.

One more point which I would like to share with you guys is that if you use new or const keyword while declaring widgets, you are also able to see the + icon which you can use to collapse and expand the widget body code. This is useful when you want to collapse/hide the rest widget code in dart file while working on another widget code in the same file.

Rahul Sharma
  • 12,515
  • 6
  • 21
  • 30
  • Good catch on that collapse thing – MoGa Nov 28 '18 at 18:23
  • 2
    The collapsing behavior would be IDE-specific. You should explicitly state which one(s) it applies to. – jamesdlin Apr 02 '19 at 15:23
  • 3
    Also, "calling a class" (by which I assume you mean "invoking a class's constructor") does not always return a new instance of the class. If you invoke a *factory* constructor, you might get back an *existing* instance. This is why `new` is discouraged: using it looks like it unconditionally creates a new instance when it might not. – jamesdlin Aug 29 '19 at 21:04