title: Dart 内置类型date: 2021-07-15 11:01:08.916

updated: 2021-07-15 12:40:46.16
url: /?p=297
categories: Dart
tags:

内置的类型

与Java的八大内置基本数据类型不同,Dart的类型都是类,Dart 内置支持下面这些类型:

  • numbers
  • strings
  • booleans
  • lists (也被称之为 arrays)
  • maps
  • runes (用于在字符串中表示 Unicode 字符)
  • symbols

Numbers(数值)

num是数字类型的父类,有两个子类intdouble

int

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
part of dart.core;

/// An integer number.
///
/// The default implementation of `int` is 64-bit two's complement integers
/// with operations that wrap to that range on overflow.
///
/// **Note:** When compiling to JavaScript, integers are restricted to values
/// that can be represented exactly by double-precision floating point values.
/// The available integer values include all integers between -2^53 and 2^53,
/// and some integers with larger magnitude. That includes some integers larger
/// than 2^63.
/// The behavior of the operators and methods in the [int]
/// class therefore sometimes differs between the Dart VM and Dart code
/// compiled to JavaScript. For example, the bitwise operators truncate their
/// operands to 32-bit integers when compiled to JavaScript.
///
/// Classes cannot extend, implement, or mix in `int`.
abstract class int extends num {
...
}
  1. int类型继承自num类型。
  2. int类型表示一个整型。
  3. int类型占用多少个字节应该看运行环境而定。但是最大还是8个字节。
  4. Dart中的int可以当成Java中的short&int&long,如果数字比较小,相当于使用java中的short&int,超过4个字节,那么就确定为long类型,我个人认为它是动态确定的。

double

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

part of dart.core;

/// A double-precision floating point number.
///
/// Representation of Dart doubles containing double specific constants
/// and operations and specializations of operations inherited from
/// [num]. Dart doubles are 64-bit floating-point numbers as specified in the
/// IEEE 754 standard.
///
/// The [double] type is contagious. Operations on [double]s return
/// [double] results.
///
/// It is a compile-time error for a class to attempt to extend or implement
/// double.
abstract class double extends num {
...
}

Strings(字符串)

Dart 字符串是 UTF-16 编码的字符序列。 可以使用单引号或者双引号来创建字符串,单引号和双引号可以嵌套使用,否则需要使用\进行转义。字符串中也可以引用变量与表达式。

1
2
3
4
var name = 'lautung';
//如果插入一个简单的标识符,而后面没有紧跟更多的字母数字文本,那么{}应该被省略。
var a = "my name is $name!";
var b = "my name is ${name.toUpperCase()}!";
1
2
3
4
// 单双引号声明
String s1 = "\"test\"";
String s2 = '"test"';
String s3 = "'test'";

与Java一样可以使用 + 操作符来把拼接字符串,也可以把多个 字符串放到一起来实现同样的功能:

1
var a  = "my name is " "lance";

使用三个单引号或者双引号可以 创建多行字符串对象

1
2
3
4
5
6
7
var s1 = '''
You can create
multi-line strings like this one.
''';

var s2 = """This is also a
multi-line string.""";
提供一个 `r` 前缀可以创建一个 “原始 raw” 字符串
1
2
print(r"换行符:\n"); // 换行符:\n  r:不需要转义
print("换行符:\\n"); // 换行符:\n

Booleans(布尔值)

Dart 有一个名字为 bool 的类型。 只有两个对象是布尔类型的:truefalse 。这一点和Java没有太大的区别。

Lists(列表&数组)

几乎所有编程语言中最常见的集合可能是数组或有序对象组。在Dart中,数组就是List对象。对List进行遍历也和Java一样。

1
2
3
4
5
6
7
8
9
10
11
12
13
var list = [1, 2, 3];
//Lists 的下标索引从 0 开始,第一个元素的索引是 0. list.length - 1 是最后一个元素的索引
print(list[list.length-1]);
//修改元素
list[0] = 2;

//使用new(实际上new可以省去)
var list = new List(1);
list[0] = 2;

//在 list 字面量之前添加 const 关键字,可以 定义一个不变的 list 对象(编译时常量)
var list = const [1,2,3];
i.add(2); ///错误,list不可变

遍历数组:

1
2
3
4
5
// 遍历数组
//iter for-in 模板
for (var o in list1) {}
//itar for-i模板
for (var j = 0; j < list1.length; ++j) {}

const list:

1
2
3
4
5
// list对象不可变
// const 修饰的是[1,2,3]这一个对象,表示这个对象不可变,不能再add元素了
List<int> list2 = const [1, 2, 3];
// list2.add(1); todo 错误
list2 = [2, 3, 4];
1
2
3
4
5
//const 修饰的是变量,也就是引用,那么变量就不能够再重新引用其他的对象了,
// 也不允许add元素了
const List<int> list3 = [1, 2, 3];
// list3 = [3,4]; todo 错误
// list3.add(11); todo 错误

Maps(映射集合)

Map:键值对相关的对象。 键和值可以是任何类型的对象。每个 键 只出现一次, 而一个值则可以出现多次。

1
2
3
4
5
6
7
8
9
10
11
12
//直接声明,用{}表示,里面写key和value,每组键值对中间用逗号隔开
var companys = {'a': '阿里巴巴', 't': '腾讯', 'b': '百度'};
var companys2 = new Map();
companys2['a'] = '阿里巴巴';
companys2['t'] = '腾讯';
companys2['b'] = '百度';

//添加元素
companys['j'] = '京东';
//获取与修改元素
var c = companys['c']; ///没有对应的key返回null
companys['a'] = 'alibaba';

与List一样,在 map字面量之前添加 const 关键字,可以 定义一个 编译时常量 的 map

Runes(用于在字符串中表示Unicode字符)

如果需要获得特殊字符的Unicode编码,或者需要将32位的Unicode编码转换为字符串,就可以借助Runes类。

Dart表达Unicode代码点的常用方法是\uXXXX,其中XXXX是4位十六进制值。要指定多于或少于4个十六进制数字,需要将值放在大括号中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var clapping = '\u{1f44f}'; ///5个16进制 需要使用{}
print(clapping);//👏
//获得 16位代码单元
print(clapping.codeUnits); //[55357, 56399]
//获得unicode代码
print(clapping.runes.toList()); //[128079]

//fromCharCode 根据字符码创建字符串
print( String.fromCharCode(128079));
print( String.fromCharCodes(clapping.runes));
print( String.fromCharCodes([55357, 56399]));
print( String.fromCharCode(0x1f44f));

Runes input = new Runes(
'\u2665 \u{1f605} \u{1f60e} \u{1f47b} \u{1f596} \u{1f44d}');
print(String.fromCharCodes(input));

实际上在Flutter开发中Runes与下一个Symbols可能永远也不会用到。

Symbols

操作符标识符,可以看作C中的宏。表示编译时的一个常量
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var i = #A; //常量

main() {
print(i);
switch(i){
case #A:
print("A");
break;
case #B:
print("B");
break;

}
var b = new Symbol("b");
print(#b == b); ///true
}