- 基本数据类型:
• int:整数类型,例如 int age = 25;
• double:双精度浮点数类型,例如 double price = 99.99;
• bool:布尔类型,表示 true 或 false,例如 bool isActive = true;
• String:字符串类型,例如 String name = “Flutter”;
- 集合类型:
• List:列表(数组),例如 List<String> names = [“Alice”, “Bob”];
• Set:集合,不允许重复元素,例如 Set<int> ids = {1, 2, 3};
• Map:映射(字典),例如 Map<String, int> ages = {“Alice”: 25, “Bob”: 30};
- 动态类型:
• var:自动推断类型,第一次赋值确定类型,例如 var count = 10;
• dynamic:动态类型,可以随时改变类型,例如 dynamic data = “Hello”; data = 123;
• Object:所有 Dart 对象的基类,可以存储任何对象,但不同于 dynamic,不会自动推断类型。
- 空安全类型:
• 可以使用 ? 声明一个可空类型,例如 int? age = null; 表示 age 可以是 int 类型或者 null。
• 可以使用 late 关键字延迟初始化,例如 late String description; 表示 description 会稍后赋值,但在使用之前必须被初始化。
- 类型别名(Type Aliases):
• 使用 typedef 定义类型别名,例如 typedef IntList = List<int>; 可以将 List<int> 简写为 IntList。
- 函数类型:
• Dart 支持将函数作为变量类型,例如 void Function(int) 或 int Function(int, int) 来表示函数类型。
- 泛型类型:
• Dart 支持泛型,例如 List<T>、Map<K, V> 等,使类型更具通用性。