寫在前面
本文基本上是將dart官網(wǎng)部分內(nèi)容進行翻譯,沒興趣的請出門左轉(zhuǎn)至Dart的官網(wǎng),有興趣的同志請繼續(xù)閱讀本文。
Flutter教程在這里
數(shù)字
Dart的數(shù)字一共分為2類:
-
int
整型不大于64位,具體取決于平臺。 在Dart VM上,值可以是-263到263 - 1.編譯為JavaScript的Dart使用JavaScript 數(shù)字,允許從-253到253 - 1的值。 -
double
64位(雙精度)浮點數(shù),由IEEE 754標準規(guī)定。
int和double都是num的子類型。 num類型包括基本運算符,如+, - ,/和*,也是你可以找到abs(),ceil()和floor()以及其他方法的地方。 (按位運算符,如>>,在int類中有定義。)如果num及其子類沒有您要想要內(nèi)容,那dart:math庫可能有您想要的。
整數(shù)是沒有小數(shù)點的數(shù)字。 以下是定義整數(shù)文字的一些示例:
int x = 1;
int hex = 0xDEADBEEF;
如果數(shù)字包含小數(shù),則為雙精度數(shù)。 以下是定義雙精度數(shù)字的一些示例:
double y = 1.1;
double exponents = 1.42e5;
以下是將字符串轉(zhuǎn)換為數(shù)字的方法,反之亦然:
// 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');
int類型指定傳統(tǒng)的按位移位(<<,>>),AND(&)和OR(|)運算符。 例如:
assert((3 << 1) == 6); // 0011 << 1 == 0110
assert((3 >> 1) == 1); // 0011 >> 1 == 0001
assert((3 | 4) == 7); // 0011 | 0100 == 0111
文字數(shù)字是編譯時常量。 許多算術(shù)表達式也是編譯時常量,只要它們的操作數(shù)是編譯為數(shù)字的編譯時常量。(注:這是說如果一個表達式涉及到的變量也是編譯時常量,那么表達式也是編譯時常量。)
const msPerSecond = 1000;
const secondsUntilRetry = 5;
const msUntilRetry = secondsUntilRetry * msPerSecond;
字符串
Dart字符串是一系列UTF-16代碼單元。 您可以使用單引號或雙引號來創(chuàng)建字符串:
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}將表達式的值放在字符串中。 如果表達式是標識符,則可以跳過{}。 要獲取對應(yīng)于對象的字符串,Dart調(diào)用對象的toString()方法。
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!');
注意:==運算符測試兩個對象是否相同。 如果兩個字符串包含相同的代碼單元序列,則它們是等效的。
您可以使用相鄰的字符串文字或+運算符來連接字符串:
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.');
創(chuàng)建多行字符串的另一種方法:使用帶有單引號或雙引號的三重引號:
var s1 = '''
You can create
multi-line strings like this one.
''';
var s2 = """This is also a
multi-line string.""";
您可以通過在其前面加上r來創(chuàng)建“raw”字符串:
var s = r'In a raw string, not even \n gets special treatment.';
有關(guān)如何在字符串中表示Unicode字符的詳細信息,請參閱Runes。
文字字符串是編譯時常量,只要任何插值表達式是一個編譯時常量,其值為null或數(shù)值,字符串或布爾值。
// 這些在const String可以使用.
const aConstNum = 0;
const aConstBool = true;
const aConstString = 'a constant string';
// 這些在const String中不能使用.
var aNum = 0;
var aBool = true;
var aString = 'a string';
const aConstList = [1, 2, 3];
const validConstString = '$aConstNum $aConstBool $aConstString';
// const invalidConstString = '$aNum $aBool $aString $aConstList';
有關(guān)使用字符串的更多信息,請參閱字符串和正則表達式。
布爾值
為了表示布爾值,Dart有一個名為bool的類型。 只有兩個對象具有bool類型: true和false,它們都是編譯時常量。
Dart的類型安全意味著您不能使用if(nonbooleanValue)或assert(nonbooleanValue)等代碼。 相反,明確檢查值,如下所示:
// Check for an empty string.
var fullName = '';
assert(fullName.isEmpty);
// Check for zero.
var hitPoints = 0;
assert(hitPoints <= 0);
// Check for null.
var unicorn;
assert(unicorn == null);
// Check for NaN.
var iMeantToDoThis = 0 / 0;
assert(iMeantToDoThis.isNaN);
列表
也許幾乎每種編程語言中最常見的集合是數(shù)組或有序的對象組。 在Dart中,數(shù)組是List 對象,因此大多數(shù)人只是將它們稱為列表。
Dart列表文字看起來像JavaScript數(shù)組文字。 這是一個簡單的Dart List:
var list = [1, 2, 3];
注意:分析器推斷列表的類型為List <int>。 如果嘗試將非整數(shù)對象添加到此列表,則分析器或運行時會引發(fā)錯誤。 有關(guān)更多信息,請閱讀類型推斷。
List使用從零開始的索引,其中0是第一個元素的索引,list.length - 1是最后一個元素的索引。 您可以像在JavaScript中一樣獲取列表的長度并引用列表元素:
var list = [1, 2, 3];
assert(list.length == 3);
assert(list[1] == 2);
list[1] = 1;
assert(list[1] == 1);
要創(chuàng)建一個編譯時常量的列表,請在列表文字之前添加const:
var constantList = const [1, 2, 3];
// constantList[1] = 1; // 取消注釋會導(dǎo)致錯誤。
List類型有許多方便的方法來操作列表。 有關(guān)列表的更多信息,請參閱泛型和集合。
映射
通常,映射是一個有鍵和值的對象。 鍵和值都可以是任何類型的對象。 每個鍵只出現(xiàn)一次,但您可以多次使用相同的值。 Dart的Map支持由映射文字和Map。
這里有幾個簡單的Dart映射,使用map文字創(chuàng)建:
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>。 如果您嘗試將錯誤類型的值添加到任一映射,則分析器或運行時會引發(fā)錯誤。 有關(guān)更多信息,請閱讀類型推斷。
您可以使用Map構(gòu)造函數(shù)創(chuàng)建相同的對象:
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';
注意:您可能希望看到
new Map()而不僅僅是Map()。 從Dart 2開始,new關(guān)鍵字是可選的。 有關(guān)詳細信息,請參閱使用構(gòu)造函數(shù)。
像在JavaScript中一樣,將新的鍵值對添加到現(xiàn)有Map:
var gifts = {'first': 'partridge'};
gifts['fourth'] = 'calling birds'; // 新增一個鍵值對
以與在JavaScript中相同的方式從Map中檢索值:
var gifts = {'first': 'partridge'};
assert(gifts['first'] == 'partridge');
如果您在Map中查找一個不存的鍵,則會返回null:
var gifts = {'first': 'partridge'};
assert(gifts['fifth'] == null);
使用.length獲取Map中鍵值對的數(shù)量:
var gifts = {'first': 'partridge'};
gifts['fourth'] = 'calling birds';
assert(gifts.length == 2);
要創(chuàng)建一個為編譯時常量的Map,請在map之前添加const:
final constantMap = const {
2: 'helium',
10: 'neon',
18: 'argon',
};
// constantMap[2] = 'Helium'; // 取消注釋會導(dǎo)致錯誤。