What source code in Dart language can be considered as valid and why?

mez*_*oni 1 dart

This script (source code) can be perfectly executed in the production mode.

void main() {
  int greeting = "Hello world!";
  print(greeting);
}
Run Code Online (Sandbox Code Playgroud)

This is a traditional hello world example that works fine in Dart.

The result is "Hello world!".

This script is self enough because not required other functionality and it works as expected.

Now I have small questions:

  1. Can I consider that this script is valid and correct source code in Dart language becuase it works as expected?
  2. If this script is valid source code in Dart language then why it cannot be executed in non-production mode?
  3. If some code that can be perfectly executed in the production mode but cannot be executed in other mode then which mode in Dart more correct (production mode or other mode)?

P.S.

As a programmer, I am interesting not in theory and practice but I have interest only in answers in my small questions based on real example (even if it are very small).

If my questions are not so correct then I would like to know why?

Because they are directly related to programming in Dart language.

Gün*_*uer 6

It is valid because types are ignored in production mode.

In checked mode (intended for development only) types are checked and you get an exception.

Types in Dart are not for runtime but for development time to make tools able to reason about the code and show possible bugs. This means it doesn't matter if you type String or var. You cannot omit it completely because this violates the syntax.

It can be executed in production mode

# ~/dart/playground/bin/dart_valid
 $ dart main.dart
Hello world!
Run Code Online (Sandbox Code Playgroud)

It fails in checked mode (development mode)

# ~/dart/playground/bin/dart_valid
 $ dart -c main.dart
Unhandled exception:
type 'String' is not a subtype of type 'int' of 'greeting'.
#0      main (file:///home/zoechi/source/my/dart/playground/bin/dart_valid/main.dart:2:18)
#1      _startIsolate.isolateStartHandler (dart:isolate-patch/isolate_patch.dart:216)
#2      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:115)
Run Code Online (Sandbox Code Playgroud)

pub build fails because it uses the analyzer which utilizes type annotations like in checked mode and throws.