Built-in types-基本数据类型(Dart中文文档)

  • 初次翻译,部分内容并非按字面翻译,是按本人理解进行了内容重组。如有错误望指正。
    Dart语言内置如下数据类型:
  • numbers
  • strings
  • booleans
  • lists (所谓的数组)
  • maps
  • runes (for expressing Unicode characters in a string)
  • symbols

你可以通过直接赋值指定变量为如上几个基本数据类型,比如,‘this is a string’就是String类型的值,true就是boolean类型的值。

Numbers

Dart numbers类型由两种:

int

整型,值最大不超过64bits,占用内存大小根据各平台VM的实现来定。在DartVM中,值区间为-2的63次方到2的63次-1,在javaScript中,数值区间为-2的53次方到2的53-1次方

double

64位双精度浮点型数据,是按照IEEE 754 标准定义。

int和double都是num型的子类,num型可以支持+, -, /, 和*,还有abs(),ceil(),floor的数学类操作。其中int类型可以支持 >>类似的移位操作。如果num型数据不符合你的要求,可以在dart:math 库中需要其它数值类型。

变量赋值没有小数点时,VM自动类型判断其为int

1var x = 1; 2var hex = 0xDEADBEEF;

如果变量包含小数点,VM自动类型判断其为double

1var y = 1.1; 2var exponents = 1.42e5;

从Dart2.1开始,int型在特定条件下自动转为double型,比如下方的写法,但是在Dart2.1之前,如下写法将报错

double z = 1; // Equivalent to double z = 1.0 

如下是num和string互相转换的示例:

1// String -> int 2var one = int.parse('1'); 3assert(one == 1); 4 5// String -> double 6var onePointOne = double.parse('1.1'); 7assert(onePointOne == 1.1); 8 9// int -> String 10String oneAsString = 1.toString(); 11assert(oneAsString == '1'); 12 13// double -> String 14String piAsString = 3.14159.toStringAsFixed(2); 15assert(piAsString == '3.14');

int型数据支持位运算(<<,>>,& ,|)

1assert((3 << 1) == 6); // 0011 << 1 == 0110 2assert((3 >> 1) == 1); // 0011 >> 1 == 0001 3assert((3 | 4) == 7); // 0011 | 0100 == 0111

数值常量支持二次计算赋值(我感觉要表述的是这个意思
Literal numbers are compile-time constants. Many arithmetic expressions are also compile-time constants, as long as their operands are compile-time constants that evaluate to numbers.)

1const msPerSecond = 1000; 2const secondsUntilRetry = 5; 3const msUntilRetry = secondsUntilRetry * msPerSecond;

Strings

A Dart string is a sequence of UTF-16 code units. You can use either single or double quotes to create a string:
Dart的字符串采用的是UTF-16的编码,定义一个字符串可以采用单引号或者双引号

1var s1 = 'Single quotes work well for string literals.'; 2var s2 = "Double quotes work just as well."; 3var s3 = 'It's easy to escape the string delimiter.'; 4var s4 = "It's even easier to use the other delimiter.";

你可以在字符串中使用${expression}表达式作为动态内容,如果expression可以明确推断,可以去除{}.使用表达式时,表达式关联的对象将调用toString()方法

1var s = 'string interpolation'; 2 3assert('Dart has $s, which is very handy.' == 4 'Dart has string interpolation, ' + 5 'which is very handy.'); 6assert('That deserves all caps. ' + 7 '${s.toUpperCase()} is very handy!' == 8 'That deserves all caps. ' + 9 'STRING INTERPOLATION is very handy!');

注意:两个字符串是否相同,取决是字符串的文本是否相同,并非要求其指向相同的内存地址

你可以通过+号拼接字符串。如果是需要换行形式的字符串,可以用三个单引号或者双引号来定义字符串

1var s1 = 'String ' 2 'concatenation' 3 " works even over line breaks."; 4assert(s1 == 5 'String concatenation works even over ' 6 'line breaks.'); 7 8var s2 = 'The + operator ' + 'works, as well.'; 9assert(s2 == 'The + operator works, as well.'); 10 11var s1 = ''' 12You can create 13multi-line strings like this one. 14'''; 15 16var s2 = """This is also a 17multi-line string.""";

你可以创建一个raw格式的字符串,正常字符串采用UTF-16字符编码,raw格式采用UTF-32字符编码。(这是本人理解,不知道对不对)

1var s = r'In a raw string, not even 2 gets special treatment.'; 3See Runes for details on how to express Unicode characters in a string.

如果字符串是const常量,其中的表达式也应该是字符串常量。

1// These work in a const string. 2const aConstNum = 0; 3const aConstBool = true; 4const aConstString = 'a constant string'; 5 6// These do NOT work in a const string. 7var aNum = 0; 8var aBool = true; 9var aString = 'a string'; 10const aConstList = [1, 2, 3]; 11 12const validConstString = '$aConstNum $aConstBool $aConstString'; 13// const invalidConstString = '$aNum $aBool $aString $aConstList';

Booleans

boolean类型的值只有两种,分别是true和false,它们是内置常量。
Dart是类型安全的。这样意味者if (nonbooleanValue) ,assert (nonbooleanValue)中的值必须是boolean类型,不能是其它类型,否则编译不通过

1// Check for an empty string. 2var fullName = ''; 3assert(fullName.isEmpty); 4 5// Check for zero. 6var hitPoints = 0; 7assert(hitPoints <= 0); 8 9// Check for null. 10var unicorn; 11assert(unicorn == null); 12 13// Check for NaN. 14var iMeantToDoThis = 0 / 0; 15assert(iMeantToDoThis.isNaN);

Lists

Dart的List创建写法JavaScript的数组,下面是一个简单例子:

var list = [1, 2, 3]; 

注意:如果数组定义是,限定了类型,比如限定为int,其添加元素都必须为int,否则会报错
数组的计数采用从0开始,那么数组第一个元素定位就写为list[0],数组最后一个元素定位就协议list[list.length-1]。具体的写法和JavaScript相似。

1var list = [1, 2, 3]; 2assert(list.length == 3); 3assert(list[1] == 2); 4 5list[1] = 1; 6assert(list[1] == 1);

如果数组赋值采用const修饰,数组元素不可修改。

1var constantList = const [1, 2, 3]; 2// constantList[1] = 1; // Uncommenting this causes an error.

Maps

map类型由key和value组成,它们可以是任意类型对象,key值是不可重复的,value值可以重复。Dart支持定义一组key/value来创建map对象,类型json的写法。

1var gifts = { 2 // Key: Value 3 'first': 'partridge', 4 'second': 'turtledoves', 5 'fifth': 'golden rings' 6}; 7 8var nobleGases = { 9 2: 'helium', 10 10: 'neon', 11 18: 'argon', 12};

注意:代码分析器会根据key/value值,判断map对象的类型,比如,gifts判定为Map<String,String>,nobleGases判定了Map<int,String>,如果添加其它类型的key/value可能导致编译出错或者运行出错。

你可以通过Map构造器创建Map对象,如下图所示:

1var gifts = Map(); 2gifts['first'] = 'partridge'; 3gifts['second'] = 'turtledoves'; 4gifts['fifth'] = 'golden rings'; 5 6var nobleGases = Map(); 7nobleGases[2] = 'helium'; 8nobleGases[10] = 'neon'; 9nobleGases[18] = 'argon';

对已创建的map对象添加数据写法类似JavaScript的写法,具体如下:

1var gifts = {'first': 'partridge'}; 2gifts['fourth'] = 'calling birds'; // Add a key-value pair 3 4var gifts = {'first': 'partridge'}; 5assert(gifts['first'] == 'partridge');

如果key值在map中不存在,返回null对象

1var gifts = {'first': 'partridge'}; 2assert(gifts['fifth'] == null);

你可以使用.length获取map对象的key数量。

1var gifts = {'first': 'partridge'}; 2gifts['fourth'] = 'calling birds'; 3assert(gifts.length == 2);

如果map对象是一个编译常量,不可以对map进行修改。

1final constantMap = const { 2 2: 'helium', 3 10: 'neon', 4 18: 'argon', 5}; 6// constantMap[2] = 'Helium'; // Uncommenting this causes an error.

Runes

In Dart, runes are the UTF-32 code points of a string.

在Dart中,runes是UTF-32编码的字符。Unicode对每个字符,数字,标记都定义了一个唯一编码。因为Dart的字符串是UTF-16编码,如果希望表示32位的字符编码,需要特殊写法。一般的unicode编码写法是uxxxx,xxxx是4个16进制字符。例如, (♥) is u2665.如果编码是多于或者少于4个字符,需要用大括号分割,比如 (😆) is u{1f600}。

Symbols (看不太懂)

A Symbol object represents an operator or identifier declared in a Dart program. You might never need to use symbols, but they’re invaluable for APIs that refer to identifiers by name, because minification changes identifier names but not identifier symbols.

To get the symbol for an identifier, use a symbol literal, which is just # followed by the identifier:

1#radix 2#bar

Symbol literals are compile-time constants.

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )