flutter【2】dart語言--內建資料型別
內建型別
- 數值型
- 字串
- 布林型
- 列表/陣列
- Map
- runes(表示字串中的 unicode 字元)
- symbols
上述型別的變數多可以用字面量進行初始化,例如'this is a string'
是一個字串字面量,true
是 boolean 字面量
dart中變數都是引用的物件,所以可以用構造器進行初始化,部分內建變數有自己的構造器,例如 Map()。
數值型
dart的數值型有兩種:
- int:整型,位寬由執行平臺決定,但是不大於 64 bit
- double:IEEE754 標準定義的 64 bit 雙精度浮點數
int 和 double 都是 num 的子型別,包括加減乘除(+ - * /)操作,abs(),ceil(),floor(),等方法。(位移操作 >> 在 int 中定義)。如果基礎操作滿足不了需求,可以使用dart:math 庫。
在dart2.1之後,整型字面量可以自動轉換為 double 型:double z = 1; //等價於 double z = 1.0
字串與整型互相轉換
// String -> int var one = int.parse('1'); assert(one == 1); // String -> double var onePointOne = double.parse('1.1'); assert(onePointOne == 1.1); // int -> String String oneAsString = 1.toString(); assert(oneAsString == '1'); // double -> String String piAsString = 3.14159.toStringAsFixed(2); assert(piAsString == '3.14');
整型的位運算、與運算、或運算
assert((3 << 1) == 6); // 0011 << 1 == 0110 assert((3 >> 1) == 1); // 0011 >> 1 == 0001 assert((3 | 4) == 7); // 0011 | 0100 == 0111
數值型字面量是編譯時常量。由編譯時常量組成的算術表示式也是編譯時常量。
const msPerSecond = 1000; const secondsUntilRetry = 5; const msUntilRetry = secondsUntilRetry * msPerSecond;
String
dart 使用的是 utf-16 的字符集,可以用單引號或者雙引號建立。
var s1 = 'Single quotes work well for string literals.'; var s2 = "Double quotes work just as well."; var s3 = 'It\'s easy to escape the string delimiter.'; var s4 = "It's even easier to use the other delimiter.";
使用 ${expression} 可以在字串中使用表示式的值,如果表示式就是一個識別符號,可以省略大括號。
var s = 'string interpolation'; assert('Dart has $s, which is very handy.' == 'Dart has string interpolation, ' + 'which is very handy.'); assert('That deserves all caps. ' + '${s.toUpperCase()} is very handy!' == 'That deserves all caps. ' + 'STRING INTERPOLATION is very handy!');
dart 中 == 用來判斷連個物件是否相等,字串只要字元內容相同就表示相等。
連線字串可以用 + 操作符,也可以用相鄰的字串字面量。
var s1 = 'String ' 'concatenation' " works even over line breaks."; assert(s1 == 'String concatenation works even over ' 'line breaks.'); var s2 = 'The + operator ' + 'works, as well.'; assert(s2 == 'The + operator works, as well.');
建立多行字串:使用三個單引號或者雙引號。
var s1 = ''' You can create multi-line strings like this one. '''; var s2 = """This is also a multi-line string.""";
raw字串:其中的轉義符不會被處理
var s = r'In a raw string, not even \n gets special treatment.';
更多資訊參考Strings and regular expressions
布林型
dart中用 bool 型別來表述布林資料,只有兩個物件有 bool 型別:true 和 false(boolean字面量,編譯時常量)
dart的型別安全意思是不可用如下程式碼
if(nonbooleanValue) …… assert(nonbooleanValue)
列表
dart中 List 物件表示陣列,也就是列表。
字面量列表:var list = [1, 2, 3];
, 會自動推斷為 List<int> 列表,如果後面插入非 int 型物件,將會出錯,具體資訊請參考type inference
。
列表索引從 0 開始,到 lsit.length - 1 結束。
建立編譯時常量列表需要在字面量列表前加上 const 關鍵字。var constList = const [1, 2, 3];
更多資訊請參考Generics 和Collections
Maps
key-value結構,dart 中用 Map型別表示。
var gifts = { // Key:Value 'first': 'partridge', 'second': 'turtledoves', 'fifth': 'golden rings' }; var nobleGases = { 2: 'helium', 10: 'neon', 18: 'argon', };
gifts 推斷為 Map<String, String>;nobleGases 推斷為 Map<int, String>。
可以使用 Map 的構造器建立Map物件,其中 new 關鍵字可以加也可以省略。
var gifts = Map(); gifts['first'] = 'partridge'; gifts['second'] = 'turtledoves'; gifts['fifth'] = 'golden rings'; var nobleGases = Map(); nobleGases[2] = 'helium'; nobleGases[10] = 'neon'; nobleGases[18] = 'argon';
Runes
runes是dart中表示utf-32格式字元的編碼單元。(In Dart, runes are the UTF-32 code points of a string.)
unicode 為全世界在用的所有字母、數字、符號都定義了一個獨一無二的數值,但是dart的字元序列使用的是 utf-16的編碼格式,所以表示utf-32unicode格式的值需要特殊的語法。
通常情況下一個unicode編碼格式為 \uXXXX,XXXX是4位16進位制的數值,如 \u2665,當要表示多於或者少於4位的時,用大括號包起來,如 \u{1f600}。
String 類提供了獲取 rune 資訊的屬性,比如 codeUnitAt 和 codeUnit 獲取 位元組的16-bit 編碼;使用runes屬性獲取字串的runes。
可以用如下程式碼展示runes、16位編碼單元和32位編碼單元的關係。
main() { var clapping = '\u{1f44f}'; print(clapping); print(clapping.codeUnits); print(clapping.runes.toList()); Runes input = new Runes( '\u2665\u{1f605}\u{1f60e}\u{1f47b}\u{1f596}\u{1f44d}'); print(new String.fromCharCodes(input)); }
需要注意的是操作 runes 列表時很容易因為特殊的語言、字元和操作而崩潰,具體參考How do I reverse a String in Dart?