Dart 自学笔记1

1,961 阅读1分钟

main 是程序的入口

// Define a function.
void printInteger(int aNumber) {
  print('The number is $aNumber.'); // Print to console.
}

// This is where the app starts executing.
void main() {
  var number = 42; // Declare and initialize a variable.
  printInteger(number); // Call a function.
}

如何声明变量

var name = 'Bob';
Object name = 'Bob';
String name = 'Bob';
int? lineCount;
late String description;
final name = 'Bob';  
final String nickname = 'Bobby';
const bar = 1000000; 
const double atm = 1.01325 * bar; 

late 必须在使用前初始化。

const 还可以用在值前面:

var foo = const [];
final bar = const [];

const 和 final 的区别

constants - What is the difference between the "const" and "final" keywords in Dart? - Stack Overflow