Dart 是用于构建 Flutter 应用的编程语言——谷歌设计的现代面向对象语言,专门用于构建 UI。理解 Dart 的关键特性(其语法、null safety、async 支持以及它如何编译)对于 Flutter 开发是必要的。
什么是 Dart
Dart = Google's language for Flutter (and more):
→ OBJECT-ORIENTED, statically typed (with type inference)
→ familiar C-style/Java-like syntax (easy for many developers to pick up)
→ designed for UI development; compiles to native code (mobile) and JavaScript (web)
Dart 的关键特性
// variables and types (statically typed with inference)
var name = 'Ann'; // inferred String
int age = 30; // explicit type
final pi = 3.14; // final (can't reassign)
const max = 100; // compile-time constant
// null safety — types are non-nullable by default
String name; // can't be null
String? nickname; // nullable (the ? allows null) — prevents null errors
// functions (including arrow syntax)
int add(int a, int b) => a + b;
// classes
class Person {
final String name;
Person(this.name); // concise constructor
}
