Why I cant create an object without the new keyword? – Dart 2

Just starting with dart so forgive me if this is too dumb. 😨

Is the new keyword optional in all cases? I can’t create an object without it.

Simple program to test:

void main() {
  var x = App();
}

class App {

}

Exception:

Unhandled exception:
NoSuchMethodError: Attempted to use type 'App' as a function. Since types do not define a method 'call', this is not possible. Did you intend to call the App constructor and forget the 'new' operator?
Receiver: App
Tried calling: App()
#0      Object.noSuchMethod (dart:core-patch/dart:core/object_patch.dart:46)
#1      main (file:///C:/Users/ga655/Code/test/main.dart:2:14)
#2      _startIsolate.<anonymous closure> (dart:isolate-patch/dart:isolate/isolate_patch.dart:279)
#3      _RawReceivePortImpl._handleMessage (dart:isolate-patch/dart:isolate/isolate_patch.dart:165)

Dart Version:

Dart VM version: 2.0.0-dev.49.0 (Wed Apr 18 20:41:36 2018 +0200) on "windows_x64"

Author: Fantashit

1 thought on “Why I cant create an object without the new keyword? – Dart 2

  1. You need to use the --preview-dart-2 option of the dart executable to be able to use optional new/const.

Comments are closed.