Declare a variable

In Dart, strings can be wrapped either in double quotes “” or single quotes “”.

Dart allows you to declare variables directly using the var keyword because of the type inference feature. Declaring variables with the var keyword is equivalent to declaring variables with the tag type.

var name = 'Bob';
String name = 'Bob';
Copy the code

When a variable is declared using the var keyword, if no initial value is assigned. It is declared as dynamic, representing any type. The initial value is null, and ${var.runtimeType} is used to check the data type of the runtime.

var value;  // null

value = 233;
print(value.runtimeType); // int

value = "hello world";
print(value.runtimeType); // String
Copy the code

The default value

When sound null Safety is not enabled (introduced in Dart 2.12), objects default to nullable types. Cases where the Sound NULL safety feature is turned on are analyzed in a separate opening.

The default value for variables that do not have an initial value is null; Since the sound null safety feature is now turned on by default, you need to prefix the file with // @dart=2.9.

/ / @ dart = 2.9
// https://dart.dev/null-safety/unsound-null-safety#testing-or-running-mixed-version-programs
void main() {

  int x;
  print(x); // null

  String y;
  print(y); // null
}
Copy the code

Newest variable

The late modifier was introduced in Dart 2.12 and has the following two functions:

  1. Declare a non-null variable for lazy initialization.
  2. Lazy initialization of variables (lazy initialization).

Because of a flaw in the Dart process analysis, when a global variable is not initialized (if it is), it is impossible to tell if it was assigned before use. The late modifier can be used to resolve the problem.

late String description;

// An error is reported if the late modifier is not used.
// Error: Field 'description' should be initialized because its type 'String' doesn't allow null.

// An error is also reported if the late modifier is used but runs without assigning a value before using it.
// LateInitializationError: Field 'description' has not been initialized.

void main() {
  description = 'Feijoada! ';
  print(description);
}
Copy the code

When a variable is marked as late but initialized at declaration time, the initialization code runs the first time the variable is used. Lazy initialization applies when:

  1. It is possible that this variable is not needed and is expensive to initialize.
  2. To be accessed during initialization of a variablethisProperties.
void main() {
  late String thermometer = readThermometer(); // Lazily initialized.

  /// Output order: before -> Succeeds -> after
  /// If you annotate the print statement, you won't print it anymore
  print("before");
  print(thermometer);
  print("after");
}

String readThermometer() {
  return "thermometer";
}
Copy the code

Final and Const

When a variable cannot be modified, you can use the keyword final or const to modify the variable. These two keywords can replace the var keyword or be prefixed to the type.

The similarities and differences are as follows:

  1. finalVariables can only be assigned once;
  2. Instance variables can befinalBut it can’t beconst.
  3. constThe variable is a compile-time constant (constVariables are alsofinal)
  4. finalVariables can be declared uninitialized, whileconstMust be assigned at declaration time.
  5. If you are usingconstTo modify a class variable, must be addedstaticThe keyword, that isstatic const(Note: The order cannot be reversed)
  6. althoughfinalThe object cannot be modified, but members of its object can be modified; whileconstThe members of the object cannot be modified and are immutable.
  7. The constructor can also be declared asconstThis type of constructor creates objects that are immutable.
final name = 'Bob';
final String nickname = 'Bobby';

// The value of the 'final' variable cannot be modified
name = 'Alice'; // Error: a final variable can only be set once

// We can assign to a const directly when we declare it, or we can assign to another const
const bar = 1000000; [Unit of pressure (dynes/cm2)]
const double atm = 1.01325 * bar; // Assign to other const variables (Standard atmosphere)
Copy the code

We can omit the const keyword if we assign a constant using an initialization expression, such as baz above, which omits const.

var foo = const [];
final bar = const [];
const baz = []; // Equivalent to 'const []' (Equivalent to 'const []')

// The value of variables that are not decorated with final or const can be changed, even if they previously referenced const.
foo = [1.2.3]; // foo Was const [] (Was const [])
baz = [42];      // Error: Constant variables can't be assigned a value
Copy the code