ZetCode

Dart 数据类型

最后修改日期:2025 年 6 月 4 日

Dart 是一种静态类型语言,拥有一套丰富的内置数据类型。这些类型有助于高效地组织和操作程序中的数据。

Dart 的基本数据类型包括数字、字符串、布尔值、列表、映射等。每种类型都有其特定的特性和已定义的运算。

数字:int 和 double

Dart 提供了两种数字类型:int 用于整数,double 用于浮点数。这两种类型都是 num 的子类。

main.dart
void main() {
  int age = 30;
  double price = 19.99;
  num temperature = 23.5; // Can hold both int and double
  
  print('Age: $age');
  print('Price: $price');
  print('Temperature: $temperature');
  
  // Number operations
  print('Sum: ${age + price}');
  print('Difference: ${price - age}');
}

本示例展示了基本的数字类型用法。我们声明了不同数字类型的变量并执行了算术运算。num 类型非常灵活。

$ dart main.dart
Age: 30
Price: 19.99
Temperature: 23.5
Sum: 49.99
Difference: -10.01

字符串

字符串在 Dart 中表示字符序列。它们是不可变的,可以使用单引号或双引号创建。支持字符串插值。

main.dart
void main() {
  String greeting = 'Hello';
  String name = "Alice";
  String message = '$greeting, $name! How are you?';
  
  print(message);
  print('Length: ${message.length}');
  print('Uppercase: ${message.toUpperCase()}');
  print('Contains "Alice": ${message.contains('Alice')}');
  
  // Multi-line string
  String multiLine = '''
  This is a
  multi-line
  string''';
  print(multiLine);
}

我们演示了字符串创建、插值和常用操作。三引号创建多行字符串。字符串方法提供了有用的功能。

$ dart main.dart
Hello, Alice! How are you?
Length: 23
Uppercase: HELLO, ALICE! HOW ARE YOU?
Contains "Alice": true
This is a
multi-line
string

布尔值

bool 类型表示布尔值 true 和 false。它用于条件表达式和逻辑运算。Dart 要求显式的布尔值。

main.dart
void main() {
  bool isRaining = true;
  bool isSunny = false;
  int temperature = 25;
  
  print('Is raining: $isRaining');
  print('Is sunny: $isSunny');
  
  // Logical operations
  bool niceWeather = !isRaining && temperature > 20;
  print('Nice weather: $niceWeather');
  
  // Conditional expression
  String weatherMessage = isRaining ? 'Bring umbrella' : 'Enjoy the day';
  print(weatherMessage);
}

这展示了布尔变量的声明以及在逻辑运算中的使用。三元运算符基于布尔值提供了简洁的条件表达式。

$ dart main.dart
Is raining: true
Is sunny: false
Nice weather: false
Bring umbrella

列表

列表是 Dart 中对象的有序集合。它们类似于其他语言中的数组。列表可以是固定长度或可增长的。

main.dart
void main() {
  // Growable list
  List<String> fruits = ['apple', 'banana', 'orange'];
  fruits.add('mango');
  
  // Fixed-length list
  List<int> fixedList = List.filled(3, 0);
  fixedList[1] = 42;
  
  print('Fruits: $fruits');
  print('Fixed list: $fixedList');
  
  // List operations
  print('First fruit: ${fruits.first}');
  print('Last fruit: ${fruits.last}');
  print('Sublist: ${fruits.sublist(1, 3)}');
  
  // List mapping
  var lengths = fruits.map((fruit) => fruit.length);
  print('Fruit lengths: $lengths');
}

我们创建了可增长和固定长度的列表。演示了各种列表操作,包括将元素映射到新值。

$ dart main.dart
Fruits: [apple, banana, orange, mango]
Fixed list: [0, 42, 0]
First fruit: apple
Last fruit: mango
Sublist: [banana, orange]
Fruit lengths: (5, 6, 6, 5)

映射

映射是 Dart 中键值对的集合。每个键必须是唯一的,键和值可以是任何类型。映射默认是无序的。

main.dart
void main() {
  // Map literal
  Map<String, int> ages = {
    'Alice': 30,
    'Bob': 25,
    'Charlie': 35
  };
  
  // Adding entries
  ages['David'] = 28;
  
  print('Ages: $ages');
  print('Alice\'s age: ${ages['Alice']}');
  
  // Map operations
  print('Keys: ${ages.keys}');
  print('Values: ${ages.values}');
  print('Contains key "Bob": ${ages.containsKey('Bob')}');
  
  // Iterating
  ages.forEach((name, age) => print('$name is $age years old'));
}

本示例演示了映射的创建、修改和常用操作。我们展示了如何使用 forEach 遍历映射条目。

$ dart main.dart
Ages: {Alice: 30, Bob: 25, Charlie: 35, David: 28}
Alice's age: 30
Keys: (Alice, Bob, Charlie, David)
Values: (30, 25, 35, 28)
Contains key "Bob": true
Alice is 30 years old
Bob is 25 years old
Charlie is 35 years old
David is 28 years old

最佳实践

来源

Dart 内置类型文档

本教程通过实际示例介绍了 Dart 的基本数据类型,演示了它们的用法和常用操作。

作者

我叫 Jan Bodnar,是一位充满热情的程序员,拥有丰富的编程经验。我从 2007 年开始撰写编程文章。至今,我已发表了 1,400 多篇文章和 8 本电子书。我在编程教学方面拥有十多年的经验。

列出 所有 Dart 教程