Dart 速查表
- 字串插值
- 可空變數
- 空值感知運算子
- 條件式屬性存取
- 集合常值
- 箭頭語法
- 串聯
- Getters 和 Setters
- 選用位置參數
- 具名參數
- 例外
- 在建構子中使用 this
- 初始化列表
- 具名建構子
- Factory 建構子
- 重新導向建構子
- Const 建構子
- 下一步?
Dart 語言的設計宗旨是讓來自其他語言的程式設計師容易學習,但它有一些獨特的功能。本教學課程將引導您了解這些最重要的語言功能。
本教學課程中嵌入的編輯器具有部分完成的程式碼片段。您可以使用這些編輯器來測試您的知識,方法是完成程式碼並點擊執行按鈕。這些編輯器也包含完整的測試程式碼;請勿編輯測試程式碼,但您可以自由研究它以了解測試。
如果您需要協助,請展開每個 DartPad 下方的... 的解決方案下拉式選單,以取得說明和答案。
字串插值
#若要將表達式的值放入字串中,請使用 ${expression}
。如果表達式是識別碼,您可以省略 {}
。
以下是一些使用字串插值的範例
字串 | 結果 |
---|---|
'${3 + 2}' | '5' |
'${"word".toUpperCase()}' | 'WORD' |
'$myObject' | myObject.toString() 的值 |
程式碼範例
#以下函式接受兩個整數作為參數。使其傳回一個字串,其中包含兩個整數,並以空格分隔。例如,stringify(2, 3)
應傳回 '2 3'
。
String stringify(int x, int y) {
TODO('Return a formatted string here');
}
// Tests your solution (Don't edit!):
void main() {
assert(stringify(2, 3) == '2 3',
"Your stringify method returned '${stringify(2, 3)}' instead of '2 3'");
print('Success!');
}
字串插值範例的解決方案
x
和 y
都是簡單值,而 Dart 的字串插值將處理將它們轉換為字串表示形式。您只需要使用 $
運算子在單引號內參考它們,並在它們之間加上一個空格即可
String stringify(int x, int y) {
return '$x $y';
}
可空變數
#Dart 強制執行健全的空值安全。這表示除非您明確指出可以為空值,否則值不能為空值。換句話說,類型預設為不可為空值。
例如,考慮以下程式碼。在空值安全機制下,此程式碼會傳回錯誤。int
類型的變數不能具有 null
值
int a = null; // INVALID.
建立變數時,在類型後方加上 ?
以指示變數可以為 null
int? a = null; // Valid.
您可以稍微簡化該程式碼,因為在所有 Dart 版本中,null
都是未初始化變數的預設值
int? a; // The initial value of a is null.
若要深入了解 Dart 中的空值安全,請閱讀健全的空值安全指南。
程式碼範例
#在此 DartPad 中宣告兩個變數
- 一個名為
name
的可空String
,值為'Jane'
。 - 一個名為
address
的可空String
,值為null
。
忽略 DartPad 中的所有初始錯誤。
// TODO: Declare the two variables here
// Tests your solution (Don't edit!):
void main() {
try {
if (name == 'Jane' && address == null) {
// verify that "name" is nullable
name = null;
print('Success!');
} else {
print('Not quite right, try again!');
}
} catch (e) {
print('Exception: ${e.runtimeType}');
}
}
可空變數範例的解決方案
將兩個變數宣告為 String
,後接 ?
。然後,將 'Jane'
指派給 name
,並讓 address
保持未初始化狀態
String? name = 'Jane';
String? address;
空值感知運算子
#Dart 提供了一些方便的運算子來處理可能為空值的值。其中一個是 ??=
指派運算子,它僅在變數目前為空值時才將值指派給變數
int? a; // = null
a ??= 3;
print(a); // <-- Prints 3.
a ??= 5;
print(a); // <-- Still prints 3.
另一個空值感知運算子是 ??
,它會傳回左側的表達式,除非該表達式的值為空值,在這種情況下,它會評估並傳回右側的表達式
print(1 ?? 3); // <-- Prints 1.
print(null ?? 12); // <-- Prints 12.
程式碼範例
#嘗試代入 ??=
和 ??
運算子,以在以下程式碼片段中實作描述的行為。
忽略 DartPad 中的所有初始錯誤。
String? foo = 'a string';
String? bar; // = null
// Substitute an operator that makes 'a string' be assigned to baz.
String? baz = foo /* TODO */ bar;
void updateSomeVars() {
// Substitute an operator that makes 'a string' be assigned to bar.
bar /* TODO */ 'a string';
}
// Tests your solution (Don't edit!):
void main() {
try {
updateSomeVars();
if (foo != 'a string') {
print('Looks like foo somehow ended up with the wrong value.');
} else if (bar != 'a string') {
print('Looks like bar ended up with the wrong value.');
} else if (baz != 'a string') {
print('Looks like baz ended up with the wrong value.');
} else {
print('Success!');
}
} catch (e) {
print('Exception: ${e.runtimeType}.');
}
}
空值感知運算子範例的解決方案
在此練習中,您只需要將 TODO
註解替換為 ??
或 ??=
即可。閱讀上面的文字,確保您了解這兩者,然後嘗試一下
// Substitute an operator that makes 'a string' be assigned to baz.
String? baz = foo ?? bar;
void updateSomeVars() {
// Substitute an operator that makes 'a string' be assigned to bar.
bar ??= 'a string';
}
條件式屬性存取
#若要保護對可能為空值的物件的屬性或方法的存取,請在點 (.
) 之前加上問號 (?
)
myObject?.someProperty
上述程式碼等同於以下程式碼
(myObject != null) ? myObject.someProperty : null
您可以在單一表達式中串連多個 ?.
的用法
myObject?.someProperty?.someMethod()
如果 myObject
或 myObject.someProperty
為空值,則上述程式碼會傳回空值 (且永遠不會呼叫 someMethod()
)。
程式碼範例
#以下函式接受一個可空字串作為參數。嘗試使用條件式屬性存取使其傳回 str
的大寫版本,如果 str
為 null
,則傳回 null
。
String? upperCaseIt(String? str) {
// TODO: Try conditionally accessing the `toUpperCase` method here.
}
// Tests your solution (Don't edit!):
void main() {
try {
String? one = upperCaseIt(null);
if (one != null) {
print('Looks like you\'re not returning null for null inputs.');
} else {
print('Success when str is null!');
}
} catch (e) {
print('Tried calling upperCaseIt(null) and got an exception: \n ${e.runtimeType}.');
}
try {
String? two = upperCaseIt('a string');
if (two == null) {
print('Looks like you\'re returning null even when str has a value.');
} else if (two != 'A STRING') {
print('Tried upperCaseIt(\'a string\'), but didn\'t get \'A STRING\' in response.');
} else {
print('Success when str is not null!');
}
} catch (e) {
print('Tried calling upperCaseIt(\'a string\') and got an exception: \n ${e.runtimeType}.');
}
}
條件式屬性存取範例的解決方案
如果此練習要求您有條件地將字串轉換為小寫,您可以這樣做:str?.toLowerCase()
。使用等效的方法將字串轉換為大寫!
String? upperCaseIt(String? str) {
return str?.toUpperCase();
}
集合常值
#Dart 內建支援列表、Map 和 Set。您可以使用常值來建立它們
final aListOfStrings = ['one', 'two', 'three'];
final aSetOfStrings = {'one', 'two', 'three'};
final aMapOfStringsToInts = {'one': 1, 'two': 2, 'three': 3};
Dart 的類型推斷可以為您指派這些變數的類型。在此情況下,推斷的類型為 List<String>
、Set<String>
和 Map<String, int>
。
或者您可以自行指定類型
final aListOfInts = <int>[];
final aSetOfInts = <int>{};
final aMapOfIntToDouble = <int, double>{};
當您使用子類型的內容初始化列表,但仍希望列表為 List<BaseType>
時,指定類型非常方便
final aListOfBaseType = <BaseType>[SubType(), SubType()];
程式碼範例
#嘗試將以下變數設定為指示的值。替換現有的空值。
// Assign this a list containing 'a', 'b', and 'c' in that order:
final aListOfStrings = null;
// Assign this a set containing 3, 4, and 5:
final aSetOfInts = null;
// Assign this a map of String to int so that aMapOfStringsToInts['myKey'] returns 12:
final aMapOfStringsToInts = null;
// Assign this an empty List<double>:
final anEmptyListOfDouble = null;
// Assign this an empty Set<String>:
final anEmptySetOfString = null;
// Assign this an empty Map of double to int:
final anEmptyMapOfDoublesToInts = null;
// Tests your solution (Don't edit!):
void main() {
final errs = <String>[];
if (aListOfStrings is! List<String>) {
errs.add('aListOfStrings should have the type List<String>.');
} else if (aListOfStrings.length != 3) {
errs.add('aListOfStrings has ${aListOfStrings.length} items in it, \n rather than the expected 3.');
} else if (aListOfStrings[0] != 'a' || aListOfStrings[1] != 'b' || aListOfStrings[2] != 'c') {
errs.add('aListOfStrings doesn\'t contain the correct values (\'a\', \'b\', \'c\').');
}
if (aSetOfInts is! Set<int>) {
errs.add('aSetOfInts should have the type Set<int>.');
} else if (aSetOfInts.length != 3) {
errs.add('aSetOfInts has ${aSetOfInts.length} items in it, \n rather than the expected 3.');
} else if (!aSetOfInts.contains(3) || !aSetOfInts.contains(4) || !aSetOfInts.contains(5)) {
errs.add('aSetOfInts doesn\'t contain the correct values (3, 4, 5).');
}
if (aMapOfStringsToInts is! Map<String, int>) {
errs.add('aMapOfStringsToInts should have the type Map<String, int>.');
} else if (aMapOfStringsToInts['myKey'] != 12) {
errs.add('aMapOfStringsToInts doesn\'t contain the correct values (\'myKey\': 12).');
}
if (anEmptyListOfDouble is! List<double>) {
errs.add('anEmptyListOfDouble should have the type List<double>.');
} else if (anEmptyListOfDouble.isNotEmpty) {
errs.add('anEmptyListOfDouble should be empty.');
}
if (anEmptySetOfString is! Set<String>) {
errs.add('anEmptySetOfString should have the type Set<String>.');
} else if (anEmptySetOfString.isNotEmpty) {
errs.add('anEmptySetOfString should be empty.');
}
if (anEmptyMapOfDoublesToInts is! Map<double, int>) {
errs.add('anEmptyMapOfDoublesToInts should have the type Map<double, int>.');
} else if (anEmptyMapOfDoublesToInts.isNotEmpty) {
errs.add('anEmptyMapOfDoublesToInts should be empty.');
}
if (errs.isEmpty) {
print('Success!');
} else {
errs.forEach(print);
}
// ignore_for_file: unnecessary_type_check
}
集合常值範例的解決方案
在每個等號後方新增列表、Set 或 Map 常值。請記住為空宣告指定類型,因為它們無法推斷。
// Assign this a list containing 'a', 'b', and 'c' in that order:
final aListOfStrings = ['a', 'b', 'c'];
// Assign this a set containing 3, 4, and 5:
final aSetOfInts = {3, 4, 5};
// Assign this a map of String to int so that aMapOfStringsToInts['myKey'] returns 12:
final aMapOfStringsToInts = {'myKey': 12};
// Assign this an empty List<double>:
final anEmptyListOfDouble = <double>[];
// Assign this an empty Set<String>:
final anEmptySetOfString = <String>{};
// Assign this an empty Map of double to int:
final anEmptyMapOfDoublesToInts = <double, int>{};
箭頭語法
#您可能在 Dart 程式碼中看過 =>
符號。此箭頭語法是一種定義函式的方式,該函式會執行其右側的表達式並傳回其值。
例如,請考慮對 List
類別的 any()
方法的此呼叫
bool hasEmpty = aListOfStrings.any((s) {
return s.isEmpty;
});
以下是撰寫該程式碼的更簡單方法
bool hasEmpty = aListOfStrings.any((s) => s.isEmpty);
程式碼範例
#嘗試完成以下使用箭頭語法的陳述式。
class MyClass {
int value1 = 2;
int value2 = 3;
int value3 = 5;
// Returns the product of the above values:
int get product => TODO();
// Adds 1 to value1:
void incrementValue1() => TODO();
// Returns a string containing each item in the
// list, separated by commas (e.g. 'a,b,c'):
String joinWithCommas(List<String> strings) => TODO();
}
// Tests your solution (Don't edit!):
void main() {
final obj = MyClass();
final errs = <String>[];
try {
final product = obj.product;
if (product != 30) {
errs.add('The product property returned $product \n instead of the expected value (30).');
}
} catch (e) {
print('Tried to use MyClass.product, but encountered an exception: \n ${e.runtimeType}.');
return;
}
try {
obj.incrementValue1();
if (obj.value1 != 3) {
errs.add('After calling incrementValue, value1 was ${obj.value1} \n instead of the expected value (3).');
}
} catch (e) {
print('Tried to use MyClass.incrementValue1, but encountered an exception: \n ${e.runtimeType}.');
return;
}
try {
final joined = obj.joinWithCommas(['one', 'two', 'three']);
if (joined != 'one,two,three') {
errs.add('Tried calling joinWithCommas([\'one\', \'two\', \'three\']) \n and received $joined instead of the expected value (\'one,two,three\').');
}
} catch (e) {
print('Tried to use MyClass.joinWithCommas, but encountered an exception: \n ${e.runtimeType}.');
return;
}
if (errs.isEmpty) {
print('Success!');
} else {
errs.forEach(print);
}
}
箭頭語法範例的解決方案
對於乘積,您可以使用 *
將三個值相乘在一起。對於 incrementValue1
,您可以使用遞增運算子 (++
)。對於 joinWithCommas
,請使用 List
類別中找到的 join
方法。
class MyClass {
int value1 = 2;
int value2 = 3;
int value3 = 5;
// Returns the product of the above values:
int get product => value1 * value2 * value3;
// Adds 1 to value1:
void incrementValue1() => value1++;
// Returns a string containing each item in the
// list, separated by commas (e.g. 'a,b,c'):
String joinWithCommas(List<String> strings) => strings.join(',');
}
串聯
#若要在同一個物件上執行一系列運算,請使用串聯 (..
)。我們都看過像這樣的表達式
myObject.someMethod()
它會在 myObject
上調用 someMethod()
,而表達式的結果是 someMethod()
的傳回值。
以下是使用串聯的相同表達式
myObject..someMethod()
雖然它仍然在 myObject
上調用 someMethod()
,但表達式的結果不是傳回值,而是對 myObject
的參考!
使用串聯,您可以串連原本需要個別陳述式的運算。例如,請考慮以下程式碼,它使用條件式成員存取運算子 (?.
) 來讀取 button
的屬性 (如果它不是 null
)
final button = web.document.querySelector('#confirm');
button?.textContent = 'Confirm';
button?.classList.add('important');
button?.onClick.listen((e) => web.window.alert('Confirmed!'));
button?.scrollIntoView();
若要改為使用串聯,您可以從空值短路串聯 (?..
) 開始,這可確保不會在 null
物件上嘗試任何串聯運算。使用串聯可以縮短程式碼,並使 button
變數變得不必要
web.document.querySelector('#confirm')
?..textContent = 'Confirm'
..classList.add('important')
..onClick.listen((e) => web.window.alert('Confirmed!'))
..scrollIntoView();
程式碼範例
#使用串聯來建立單一陳述式,將 BigObject
的 anInt
、aString
和 aList
屬性設定為 1
、'String!'
和 [3.0]
(分別),然後呼叫 allDone()
。
class BigObject {
int anInt = 0;
String aString = '';
List<double> aList = [];
bool _done = false;
void allDone() {
_done = true;
}
}
BigObject fillBigObject(BigObject obj) {
// Create a single statement that will update and return obj:
return TODO('obj..');
}
// Tests your solution (Don't edit!):
void main() {
BigObject obj;
try {
obj = fillBigObject(BigObject());
} catch (e) {
print('Caught an exception of type ${e.runtimeType} \n while running fillBigObject');
return;
}
final errs = <String>[];
if (obj.anInt != 1) {
errs.add(
'The value of anInt was ${obj.anInt} \n rather than the expected (1).');
}
if (obj.aString != 'String!') {
errs.add(
'The value of aString was \'${obj.aString}\' \n rather than the expected (\'String!\').');
}
if (obj.aList.length != 1) {
errs.add(
'The length of aList was ${obj.aList.length} \n rather than the expected value (1).');
} else {
if (obj.aList[0] != 3.0) {
errs.add(
'The value found in aList was ${obj.aList[0]} \n rather than the expected (3.0).');
}
}
if (!obj._done) {
errs.add('It looks like allDone() wasn\'t called.');
}
if (errs.isEmpty) {
print('Success!');
} else {
errs.forEach(print);
}
}
串聯範例的解決方案
此練習的最佳解決方案以 obj..
開頭,並具有四個串連在一起的指派運算。從 return obj..anInt = 1
開始,然後新增另一個串聯 (..
) 並開始下一個指派。
BigObject fillBigObject(BigObject obj) {
return obj
..anInt = 1
..aString = 'String!'
..aList.add(3)
..allDone();
}
Getters 和 Setters
#每當您需要比簡單欄位允許的更多控制權時,都可以定義 getters 和 setters。
例如,您可以確保屬性的值有效
class MyClass {
int _aProperty = 0;
int get aProperty => _aProperty;
set aProperty(int value) {
if (value >= 0) {
_aProperty = value;
}
}
}
您也可以使用 getter 來定義計算屬性
class MyClass {
final List<int> _values = [];
void addValue(int value) {
_values.add(value);
}
// A computed property.
int get count {
return _values.length;
}
}
程式碼範例
#假設您有一個購物車類別,其中保留了價格的私有 List<double>
。新增以下內容
- 一個名為
total
的 getter,它傳回價格的總和 - 一個 setter,它會將列表替換為新的列表,前提是新列表不包含任何負價格 (在這種情況下,setter 應擲回
InvalidPriceException
)。
忽略 DartPad 中的所有初始錯誤。
class InvalidPriceException {}
class ShoppingCart {
List<double> _prices = [];
// TODO: Add a "total" getter here:
// TODO: Add a "prices" setter here:
}
// Tests your solution (Don't edit!):
void main() {
var foundException = false;
try {
final cart = ShoppingCart();
cart.prices = [12.0, 12.0, -23.0];
} on InvalidPriceException {
foundException = true;
} catch (e) {
print('Tried setting a negative price and received a ${e.runtimeType} \n instead of an InvalidPriceException.');
return;
}
if (!foundException) {
print('Tried setting a negative price \n and didn\'t get an InvalidPriceException.');
return;
}
final secondCart = ShoppingCart();
try {
secondCart.prices = [1.0, 2.0, 3.0];
} catch(e) {
print('Tried setting prices with a valid list, \n but received an exception: ${e.runtimeType}.');
return;
}
if (secondCart._prices.length != 3) {
print('Tried setting prices with a list of three values, \n but _prices ended up having length ${secondCart._prices.length}.');
return;
}
if (secondCart._prices[0] != 1.0 || secondCart._prices[1] != 2.0 || secondCart._prices[2] != 3.0) {
final vals = secondCart._prices.map((p) => p.toString()).join(', ');
print('Tried setting prices with a list of three values (1, 2, 3), \n but incorrect ones ended up in the price list ($vals) .');
return;
}
var sum = 0.0;
try {
sum = secondCart.total;
} catch (e) {
print('Tried to get total, but received an exception: ${e.runtimeType}.');
return;
}
if (sum != 6.0) {
print('After setting prices to (1, 2, 3), total returned $sum instead of 6.');
return;
}
print('Success!');
}
Getters 和 Setters 範例的解決方案
有兩個函式在此練習中非常方便。一個是 fold
,它可以將列表縮減為單一值 (使用它來計算總計)。另一個是 any
,它可以使用您提供的函式檢查列表中的每個項目 (使用它來檢查價格 setter 中是否有任何負價格)。
// Add a "total" getter here:
double get total => _prices.fold(0, (e, t) => e + t);
// Add a "prices" setter here:
set prices(List<double> value) {
if (value.any((p) => p < 0)) {
throw InvalidPriceException();
}
_prices = value;
}
選用位置參數
#Dart 有兩種函式參數:位置參數和具名參數。位置參數是您可能熟悉的那種
int sumUp(int a, int b, int c) {
return a + b + c;
}
// ···
int total = sumUp(1, 2, 3);
使用 Dart,您可以透過將位置參數包裝在方括號中,使其成為選用參數
int sumUpToFive(int a, [int? b, int? c, int? d, int? e]) {
int sum = a;
if (b != null) sum += b;
if (c != null) sum += c;
if (d != null) sum += d;
if (e != null) sum += e;
return sum;
}
// ···
int total = sumUpToFive(1, 2);
int otherTotal = sumUpToFive(1, 2, 3, 4, 5);
選用位置參數始終位於函式參數列表的最後。它們的預設值為 null,除非您提供另一個預設值
int sumUpToFive(int a, [int b = 2, int c = 3, int d = 4, int e = 5]) {
// ···
}
void main() {
int newTotal = sumUpToFive(1);
print(newTotal); // <-- prints 15
}
程式碼範例
#實作一個名為 joinWithCommas()
的函式,它接受一到五個整數,然後傳回以逗號分隔的這些數字的字串。以下是一些函式呼叫和傳回值的範例
函式呼叫 | 傳回值 |
---|---|
joinWithCommas(1) | '1' |
joinWithCommas(1, 2, 3) | '1,2,3' |
joinWithCommas(1, 1, 1, 1, 1) | '1,1,1,1,1' |
String joinWithCommas(int a, [int? b, int? c, int? d, int? e]) {
return TODO();
}
// Tests your solution (Don't edit!):
void main() {
final errs = <String>[];
try {
final value = joinWithCommas(1);
if (value != '1') {
errs.add('Tried calling joinWithCommas(1) \n and got $value instead of the expected (\'1\').');
}
} on UnimplementedError {
print('Tried to call joinWithCommas but failed. \n Did you implement the method?');
return;
} catch (e) {
print('Tried calling joinWithCommas(1), \n but encountered an exception: ${e.runtimeType}.');
return;
}
try {
final value = joinWithCommas(1, 2, 3);
if (value != '1,2,3') {
errs.add('Tried calling joinWithCommas(1, 2, 3) \n and got $value instead of the expected (\'1,2,3\').');
}
} on UnimplementedError {
print('Tried to call joinWithCommas but failed. \n Did you implement the method?');
return;
} catch (e) {
print('Tried calling joinWithCommas(1, 2 ,3), \n but encountered an exception: ${e.runtimeType}.');
return;
}
try {
final value = joinWithCommas(1, 2, 3, 4, 5);
if (value != '1,2,3,4,5') {
errs.add('Tried calling joinWithCommas(1, 2, 3, 4, 5) \n and got $value instead of the expected (\'1,2,3,4,5\').');
}
} on UnimplementedError {
print('Tried to call joinWithCommas but failed. \n Did you implement the method?');
return;
} catch (e) {
print('Tried calling stringify(1, 2, 3, 4 ,5), \n but encountered an exception: ${e.runtimeType}.');
return;
}
if (errs.isEmpty) {
print('Success!');
} else {
errs.forEach(print);
}
}
位置參數範例的解決方案
如果呼叫者未提供 b
、c
、d
和 e
參數,則它們為空值。因此,重要的是在將這些引數新增至最終字串之前,先檢查這些引數是否為 null
。
String joinWithCommas(int a, [int? b, int? c, int? d, int? e]) {
var total = '$a';
if (b != null) total = '$total,$b';
if (c != null) total = '$total,$c';
if (d != null) total = '$total,$d';
if (e != null) total = '$total,$e';
return total;
}
具名參數
#使用參數列表結尾的花括號語法,您可以定義具有名稱的參數。
具名參數是選用的,除非它們明確標記為 required
。
void printName(String firstName, String lastName, {String? middleName}) {
print('$firstName ${middleName ?? ''} $lastName');
}
void main() {
printName('Dash', 'Dartisan');
printName('John', 'Smith', middleName: 'Who');
// Named arguments can be placed anywhere in the argument list
printName('John', middleName: 'Who', 'Smith');
}
正如您可能預期的那樣,可空具名參數的預設值為 null
,但您可以提供自訂預設值。
如果參數的類型不可為空值,則您必須提供預設值 (如下列程式碼所示) 或將參數標記為 required
(如建構子章節所示)。
void printName(String firstName, String lastName, {String middleName = ''}) {
print('$firstName $middleName $lastName');
}
函式不能同時具有選用位置參數和具名參數。
程式碼範例
#將 copyWith()
實例方法新增至 MyDataObject
類別。它應該接受三個具名、可空參數
int? newInt
String? newString
double? newDouble
您的 copyWith()
方法應傳回基於目前實例的新 MyDataObject
,並將來自先前參數 (如果有的話) 的資料複製到物件的屬性中。例如,如果 newInt
不為空值,則將其值複製到 anInt
中。
忽略 DartPad 中的所有初始錯誤。
class MyDataObject {
final int anInt;
final String aString;
final double aDouble;
MyDataObject({
this.anInt = 1,
this.aString = 'Old!',
this.aDouble = 2.0,
});
// TODO: Add your copyWith method here:
}
// Tests your solution (Don't edit!):
void main() {
final source = MyDataObject();
final errs = <String>[];
try {
final copy = source.copyWith(newInt: 12, newString: 'New!', newDouble: 3.0);
if (copy.anInt != 12) {
errs.add('Called copyWith(newInt: 12, newString: \'New!\', newDouble: 3.0), \n and the new object\'s anInt was ${copy.anInt} rather than the expected value (12).');
}
if (copy.aString != 'New!') {
errs.add('Called copyWith(newInt: 12, newString: \'New!\', newDouble: 3.0), \n and the new object\'s aString was ${copy.aString} rather than the expected value (\'New!\').');
}
if (copy.aDouble != 3) {
errs.add('Called copyWith(newInt: 12, newString: \'New!\', newDouble: 3.0), \n and the new object\'s aDouble was ${copy.aDouble} rather than the expected value (3).');
}
} catch (e) {
print('Called copyWith(newInt: 12, newString: \'New!\', newDouble: 3.0) \n and got an exception: ${e.runtimeType}');
}
try {
final copy = source.copyWith();
if (copy.anInt != 1) {
errs.add('Called copyWith(), and the new object\'s anInt was ${copy.anInt} \n rather than the expected value (1).');
}
if (copy.aString != 'Old!') {
errs.add('Called copyWith(), and the new object\'s aString was ${copy.aString} \n rather than the expected value (\'Old!\').');
}
if (copy.aDouble != 2) {
errs.add('Called copyWith(), and the new object\'s aDouble was ${copy.aDouble} \n rather than the expected value (2).');
}
} catch (e) {
print('Called copyWith() and got an exception: ${e.runtimeType}');
}
if (errs.isEmpty) {
print('Success!');
} else {
errs.forEach(print);
}
}
具名參數範例的解決方案
copyWith
方法會出現在許多類別和函式庫中。您的方法應執行一些操作:使用選用具名參數、建立 MyDataObject
的新實例,以及使用來自參數的資料來填寫它 (如果參數為空值,則使用來自目前實例的資料)。這是一個獲得更多 ??
運算子練習的機會!
MyDataObject copyWith({int? newInt, String? newString, double? newDouble}) {
return MyDataObject(
anInt: newInt ?? this.anInt,
aString: newString ?? this.aString,
aDouble: newDouble ?? this.aDouble,
);
}
例外
#Dart 程式碼可以擲回和捕獲例外。與 Java 相反,Dart 的所有例外都是未檢查的。方法不會宣告它們可能擲回哪些例外,而且您不需要捕獲任何例外。
Dart 提供 Exception
和 Error
類型,但您允許擲回任何非空值物件
throw Exception('Something bad happened.');
throw 'Waaaaaaah!';
處理例外時,請使用 try
、on
和 catch
關鍵字
try {
breedMoreLlamas();
} on OutOfLlamasException {
// A specific exception
buyMoreLlamas();
} on Exception catch (e) {
// Anything else that is an exception
print('Unknown exception: $e');
} catch (e) {
// No specified type, handles all
print('Something really unknown: $e');
}
try
關鍵字的作用與大多數其他語言相同。使用 on
關鍵字按類型篩選特定例外,並使用 catch
關鍵字取得對例外物件的參考。
如果您無法完全處理例外,請使用 rethrow
關鍵字來傳播例外
try {
breedMoreLlamas();
} catch (e) {
print('I was just trying to breed llamas!');
rethrow;
}
若要執行程式碼,無論是否擲回例外,請使用 finally
try {
breedMoreLlamas();
} catch (e) {
// ... handle exception ...
} finally {
// Always clean up, even if an exception is thrown.
cleanLlamaStalls();
}
程式碼範例
#實作下方的 tryFunction()
。它應執行一個不可靠的方法,然後執行以下操作
- 如果
untrustworthy()
擲回ExceptionWithMessage
,請使用例外類型和訊息呼叫logger.logException
(嘗試使用on
和catch
)。 - 如果
untrustworthy()
擲回Exception
,請使用例外類型呼叫logger.logException
(嘗試使用on
來處理此問題)。 - 如果
untrustworthy()
擲回任何其他物件,請勿捕獲例外。 - 在所有內容都已捕獲和處理後,呼叫
logger.doneLogging
(嘗試使用finally
)。
typedef VoidFunction = void Function();
class ExceptionWithMessage {
final String message;
const ExceptionWithMessage(this.message);
}
// Call logException to log an exception, and doneLogging when finished.
abstract class Logger {
void logException(Type t, [String? msg]);
void doneLogging();
}
void tryFunction(VoidFunction untrustworthy, Logger logger) {
try {
untrustworthy();
} // Write your logic here
}
// Tests your solution (Don't edit!):
class MyLogger extends Logger {
Type? lastType;
String lastMessage = '';
bool done = false;
void logException(Type t, [String? message]) {
lastType = t;
lastMessage = message ?? lastMessage;
}
void doneLogging() => done = true;
}
void main() {
final errs = <String>[];
var logger = MyLogger();
try {
tryFunction(() => throw Exception(), logger);
if ('${logger.lastType}' != 'Exception' && '${logger.lastType}' != '_Exception') {
errs.add('Untrustworthy threw an Exception, but a different type was logged: \n ${logger.lastType}.');
}
if (logger.lastMessage != '') {
errs.add('Untrustworthy threw an Exception with no message, but a message \n was logged anyway: \'${logger.lastMessage}\'.');
}
if (!logger.done) {
errs.add('Untrustworthy threw an Exception, \n and doneLogging() wasn\'t called afterward.');
}
} catch (e) {
print('Untrustworthy threw an exception, and an exception of type \n ${e.runtimeType} was unhandled by tryFunction.');
}
logger = MyLogger();
try {
tryFunction(() => throw ExceptionWithMessage('Hey!'), logger);
if (logger.lastType != ExceptionWithMessage) {
errs.add('Untrustworthy threw an ExceptionWithMessage(\'Hey!\'), but a \n different type was logged: ${logger.lastType}.');
}
if (logger.lastMessage != 'Hey!') {
errs.add('Untrustworthy threw an ExceptionWithMessage(\'Hey!\'), but a \n different message was logged: \'${logger.lastMessage}\'.');
}
if (!logger.done) {
errs.add('Untrustworthy threw an ExceptionWithMessage(\'Hey!\'), \n and doneLogging() wasn\'t called afterward.');
}
} catch (e) {
print('Untrustworthy threw an ExceptionWithMessage(\'Hey!\'), \n and an exception of type ${e.runtimeType} was unhandled by tryFunction.');
}
logger = MyLogger();
bool caughtStringException = false;
try {
tryFunction(() => throw 'A String', logger);
} on String {
caughtStringException = true;
}
if (!caughtStringException) {
errs.add('Untrustworthy threw a string, and it was incorrectly handled inside tryFunction().');
}
logger = MyLogger();
try {
tryFunction(() {}, logger);
if (logger.lastType != null) {
errs.add('Untrustworthy didn\'t throw an Exception, \n but one was logged anyway: ${logger.lastType}.');
}
if (logger.lastMessage != '') {
errs.add('Untrustworthy didn\'t throw an Exception with no message, \n but a message was logged anyway: \'${logger.lastMessage}\'.');
}
if (!logger.done) {
errs.add('Untrustworthy didn\'t throw an Exception, \n but doneLogging() wasn\'t called afterward.');
}
} catch (e) {
print('Untrustworthy didn\'t throw an exception, \n but an exception of type ${e.runtimeType} was unhandled by tryFunction anyway.');
}
if (errs.isEmpty) {
print('Success!');
} else {
errs.forEach(print);
}
}
例外範例的解決方案
此練習看起來很棘手,但它實際上是一個大型 try
陳述式。在 try
內部呼叫 untrustworthy
,然後使用 on
、catch
和 finally
來捕獲例外並在記錄器上呼叫方法。
void tryFunction(VoidFunction untrustworthy, Logger logger) {
try {
untrustworthy();
} on ExceptionWithMessage catch (e) {
logger.logException(e.runtimeType, e.message);
} on Exception {
logger.logException(Exception);
} finally {
logger.doneLogging();
}
}
在建構子中使用 this
#Dart 提供了一個方便的捷徑,用於在建構子中將值指派給屬性:在宣告建構子時使用 this.propertyName
class MyColor {
int red;
int green;
int blue;
MyColor(this.red, this.green, this.blue);
}
final color = MyColor(80, 80, 128);
此技術也適用於具名參數。屬性名稱會成為參數的名稱
class MyColor {
...
MyColor({required this.red, required this.green, required this.blue});
}
final color = MyColor(red: 80, green: 80, blue: 80);
在上述程式碼中,red
、green
和 blue
標記為 required
,因為這些 int
值不能為空值。如果您新增預設值,則可以省略 required
MyColor([this.red = 0, this.green = 0, this.blue = 0]);
// or
MyColor({this.red = 0, this.green = 0, this.blue = 0});
程式碼範例
#將單行建構子新增至 MyClass
,該建構子使用 this.
語法來接收和指派類別的所有三個屬性的值。
忽略 DartPad 中的所有初始錯誤。
class MyClass {
final int anInt;
final String aString;
final double aDouble;
// TODO: Create the constructor here.
}
// Tests your solution (Don't edit!):
void main() {
final errs = <String>[];
try {
final obj = MyClass(1, 'two', 3);
if (obj.anInt != 1) {
errs.add('Called MyClass(1, \'two\', 3) and got an object with anInt of ${obj.anInt} \n instead of the expected value (1).');
}
if (obj.anInt != 1) {
errs.add('Called MyClass(1, \'two\', 3) and got an object with aString of \'${obj.aString}\' \n instead of the expected value (\'two\').');
}
if (obj.anInt != 1) {
errs.add('Called MyClass(1, \'two\', 3) and got an object with aDouble of ${obj.aDouble} \n instead of the expected value (3).');
}
} catch (e) {
print('Called MyClass(1, \'two\', 3) and got an exception \n of type ${e.runtimeType}.');
}
if (errs.isEmpty) {
print('Success!');
} else {
errs.forEach(print);
}
}
`this` 範例的解決方案
此練習有一個單行解決方案。宣告建構子,並依序將 this.anInt
、this.aString
和 this.aDouble
作為其參數。
MyClass(this.anInt, this.aString, this.aDouble);
初始化列表
#有時,當您實作建構子時,您需要在建構子主體執行之前執行一些設定。例如,final 欄位必須在建構子主體執行之前具有值。在初始化列表中完成此工作,該列表位於建構子的簽名及其主體之間
Point.fromJson(Map<String, double> json) : x = json['x']!, y = json['y']! {
print('In Point.fromJson(): ($x, $y)');
}
初始化列表也是放置 asserts 的方便位置,asserts 僅在開發期間執行
NonNegativePoint(this.x, this.y) : assert(x >= 0), assert(y >= 0) {
print('I just made a NonNegativePoint: ($x, $y)');
}
程式碼範例
#完成下方的 FirstTwoLetters
建構子。使用初始化列表將 word
中的前兩個字元指派給 letterOne
和 LetterTwo
屬性。如需額外加分,請新增 assert
以捕獲少於兩個字元的單字。
忽略 DartPad 中的所有初始錯誤。
class FirstTwoLetters {
final String letterOne;
final String letterTwo;
// TODO: Create a constructor with an initializer list here:
FirstTwoLetters(String word)
}
// Tests your solution (Don't edit!):
void main() {
final errs = <String>[];
try {
final result = FirstTwoLetters('My String');
if (result.letterOne != 'M') {
errs.add('Called FirstTwoLetters(\'My String\') and got an object with \n letterOne equal to \'${result.letterOne}\' instead of the expected value (\'M\').');
}
if (result.letterTwo != 'y') {
errs.add('Called FirstTwoLetters(\'My String\') and got an object with \n letterTwo equal to \'${result.letterTwo}\' instead of the expected value (\'y\').');
}
} catch (e) {
errs.add('Called FirstTwoLetters(\'My String\') and got an exception \n of type ${e.runtimeType}.');
}
bool caughtException = false;
try {
FirstTwoLetters('');
} catch (e) {
caughtException = true;
}
if (!caughtException) {
errs.add('Called FirstTwoLetters(\'\') and didn\'t get an exception \n from the failed assertion.');
}
if (errs.isEmpty) {
print('Success!');
} else {
errs.forEach(print);
}
}
初始化列表範例的解決方案
需要進行兩次指派:letterOne
應指派 word[0]
,而 letterTwo
應指派 word[1]
。
FirstTwoLetters(String word)
: assert(word.length >= 2),
letterOne = word[0],
letterTwo = word[1];
具名建構子
#為了允許類別具有多個建構子,Dart 支援具名建構子
class Point {
double x, y;
Point(this.x, this.y);
Point.origin() : x = 0, y = 0;
}
若要使用具名建構子,請使用其完整名稱調用它
final myPoint = Point.origin();
程式碼範例
#為 Color
類別提供一個名為 Color.black
的建構子,該建構子將所有三個屬性設定為零。
忽略 DartPad 中的所有初始錯誤。
class Color {
int red;
int green;
int blue;
Color(this.red, this.green, this.blue);
// TODO: Create a named constructor called "Color.black" here:
}
// Tests your solution (Don't edit!):
void main() {
final errs = <String>[];
try {
final result = Color.black();
if (result.red != 0) {
errs.add('Called Color.black() and got a Color with red equal to \n ${result.red} instead of the expected value (0).');
}
if (result.green != 0) {
errs.add('Called Color.black() and got a Color with green equal to \n ${result.green} instead of the expected value (0).');
}
if (result.blue != 0) {
errs.add('Called Color.black() and got a Color with blue equal to \n ${result.blue} instead of the expected value (0).');
}
} catch (e) {
print('Called Color.black() and got an exception of type \n ${e.runtimeType}.');
return;
}
if (errs.isEmpty) {
print('Success!');
} else {
errs.forEach(print);
}
}
具名建構子範例的解決方案
您的建構子的宣告應以 Color.black():
開頭。在初始化列表 (在冒號之後) 中,將 red
、green
和 blue
設定為 0
。
Color.black()
: red = 0,
green = 0,
blue = 0;
Factory 建構子
#Dart 支援 factory 建構子,它可以傳回子類型甚至空值。若要建立 factory 建構子,請使用 factory
關鍵字
class Square extends Shape {}
class Circle extends Shape {}
class Shape {
Shape();
factory Shape.fromTypeName(String typeName) {
if (typeName == 'square') return Square();
if (typeName == 'circle') return Circle();
throw ArgumentError('Unrecognized $typeName');
}
}
程式碼範例
#替換名為 IntegerHolder.fromList
的 factory 建構子中的行 TODO();
,以傳回以下內容
- 如果列表具有一個值,請使用該值建立
IntegerSingle
實例。 - 如果列表具有兩個值,請依序使用這些值建立
IntegerDouble
實例。 - 如果列表具有三個值,請依序使用這些值建立
IntegerTriple
實例。 - 否則,擲回
Error
。
如果您成功,主控台應顯示 Success!
。
class IntegerHolder {
IntegerHolder();
// Implement this factory constructor.
factory IntegerHolder.fromList(List<int> list) {
TODO();
}
}
class IntegerSingle extends IntegerHolder {
final int a;
IntegerSingle(this.a);
}
class IntegerDouble extends IntegerHolder {
final int a;
final int b;
IntegerDouble(this.a, this.b);
}
class IntegerTriple extends IntegerHolder {
final int a;
final int b;
final int c;
IntegerTriple(this.a, this.b, this.c);
}
// Tests your solution (Don't edit from this point to end of file):
void main() {
final errs = <String>[];
// Run 5 tests to see which values have valid integer holders
for (var tests = 0; tests < 5; tests++) {
if (!testNumberOfArgs(errs, tests)) return;
}
// The goal is no errors with values 1 to 3,
// but have errors with values 0 and 4.
// The testNumberOfArgs method adds to the errs array if
// the values 1 to 3 have an error and
// the values 0 and 4 don't have an error
if (errs.isEmpty) {
print('Success!');
} else {
errs.forEach(print);
}
}
bool testNumberOfArgs(List<String> errs, int count) {
bool _threw = false;
final ex = List.generate(count, (index) => index + 1);
final callTxt = "IntegerHolder.fromList(${ex})";
try {
final obj = IntegerHolder.fromList(ex);
final String vals = count == 1 ? "value" : "values";
// Uncomment the next line if you want to see the results realtime
// print("Testing with ${count} ${vals} using ${obj.runtimeType}.");
testValues(errs, ex, obj, callTxt);
} on Error {
_threw = true;
} catch (e) {
switch (count) {
case (< 1 && > 3):
if (!_threw) {
errs.add('Called ${callTxt} and it didn\'t throw an Error.');
}
default:
errs.add('Called $callTxt and received an Error.');
}
}
return true;
}
void testValues(List<String> errs, List<int> expectedValues, IntegerHolder obj,
String callText) {
for (var i = 0; i < expectedValues.length; i++) {
int found;
if (obj is IntegerSingle) {
found = obj.a;
} else if (obj is IntegerDouble) {
found = i == 0 ? obj.a : obj.b;
} else if (obj is IntegerTriple) {
found = i == 0
? obj.a
: i == 1
? obj.b
: obj.c;
} else {
throw ArgumentError(
"This IntegerHolder type (${obj.runtimeType}) is unsupported.");
}
if (found != expectedValues[i]) {
errs.add(
"Called $callText and got a ${obj.runtimeType} " +
"with a property at index $i value of $found " +
"instead of the expected (${expectedValues[i]}).");
}
}
}
Factory 建構子範例的解決方案
在 factory 建構子內部,檢查列表的長度,然後根據需要建立並傳回 IntegerSingle
、IntegerDouble
或 IntegerTriple
。
將 TODO();
替換為以下程式碼區塊。
switch (list.length) {
case 1:
return IntegerSingle(list[0]);
case 2:
return IntegerDouble(list[0], list[1]);
case 3:
return IntegerTriple(list[0], list[1], list[2]);
default:
throw ArgumentError("List must between 1 and 3 items. This list was ${list.length} items.");
}
重新導向建構子
#有時,建構子的唯一目的是重新導向到同一個類別中的另一個建構子。重新導向建構子的主體為空,建構子呼叫出現在冒號 (:
) 之後。
class Automobile {
String make;
String model;
int mpg;
// The main constructor for this class.
Automobile(this.make, this.model, this.mpg);
// Delegates to the main constructor.
Automobile.hybrid(String make, String model) : this(make, model, 60);
// Delegates to a named constructor
Automobile.fancyHybrid() : this.hybrid('Futurecar', 'Mark 2');
}
程式碼範例
#還記得上面的 Color
類別嗎?建立一個名為 black
的具名建構子,但不要手動指派屬性,而是將其重新導向到預設建構子,並以零作為引數。
忽略 DartPad 中的所有初始錯誤。
class Color {
int red;
int green;
int blue;
Color(this.red, this.green, this.blue);
// TODO: Create a named constructor called "black" here
// and redirect it to call the existing constructor
}
// Tests your solution (Don't edit!):
void main() {
final errs = <String>[];
try {
final result = Color.black();
if (result.red != 0) {
errs.add('Called Color.black() and got a Color with red equal to \n ${result.red} instead of the expected value (0).');
}
if (result.green != 0) {
errs.add('Called Color.black() and got a Color with green equal to \n ${result.green} instead of the expected value (0).');
}
if (result.blue != 0) {
errs.add('Called Color.black() and got a Color with blue equal to \n ${result.blue} instead of the expected value (0).');
}
} catch (e) {
print('Called Color.black() and got an exception of type ${e.runtimeType}.');
return;
}
if (errs.isEmpty) {
print('Success!');
} else {
errs.forEach(print);
}
}
重新導向建構子範例的解決方案
您的建構子應重新導向到 this(0, 0, 0)
。
Color.black() : this(0, 0, 0);
Const 建構子
#如果您的類別產生的物件永遠不會變更,您可以將這些物件設為編譯時期常數。若要執行此操作,請定義 const
建構子,並確保所有實例變數都是 final。
class ImmutablePoint {
static const ImmutablePoint origin = ImmutablePoint(0, 0);
final int x;
final int y;
const ImmutablePoint(this.x, this.y);
}
程式碼範例
#修改 Recipe
類別,使其執行個體可以成為常數,並建立一個執行以下操作的常數建構子
- 具有三個參數:
ingredients
、calories
和milligramsOfSodium
(依此順序)。 - 使用
this.
語法將參數值自動指派給同名的物件屬性。 - 是常數,
const
關鍵字位於建構子宣告中的Recipe
之前。
忽略 DartPad 中的所有初始錯誤。
class Recipe {
List<String> ingredients;
int calories;
double milligramsOfSodium;
// TODO: Create a const constructor here"
}
// Tests your solution (Don't edit!):
void main() {
final errs = <String>[];
try {
const obj = Recipe(['1 egg', 'Pat of butter', 'Pinch salt'], 120, 200);
if (obj.ingredients.length != 3) {
errs.add('Called Recipe([\'1 egg\', \'Pat of butter\', \'Pinch salt\'], 120, 200) \n and got an object with ingredient list of length ${obj.ingredients.length} rather than the expected length (3).');
}
if (obj.calories != 120) {
errs.add('Called Recipe([\'1 egg\', \'Pat of butter\', \'Pinch salt\'], 120, 200) \n and got an object with a calorie value of ${obj.calories} rather than the expected value (120).');
}
if (obj.milligramsOfSodium != 200) {
errs.add('Called Recipe([\'1 egg\', \'Pat of butter\', \'Pinch salt\'], 120, 200) \n and got an object with a milligramsOfSodium value of ${obj.milligramsOfSodium} rather than the expected value (200).');
}
} catch (e) {
print('Tried calling Recipe([\'1 egg\', \'Pat of butter\', \'Pinch salt\'], 120, 200) \n and received a null.');
}
if (errs.isEmpty) {
print('Success!');
} else {
errs.forEach(print);
}
}
Const 建構子範例的解決方案
若要使建構子成為常數,您需要使所有屬性都成為 final。
class Recipe {
final List<String> ingredients;
final int calories;
final double milligramsOfSodium;
const Recipe(this.ingredients, this.calories, this.milligramsOfSodium);
}
下一步?
#我們希望您喜歡使用本教學課程來學習或測試您對 Dart 語言一些最有趣功能的知識。
接下來您可以嘗試的包括
- 試用其他 Dart 教學課程。
- 閱讀Dart 語言導覽。
- 試玩 DartPad。
- 取得 Dart SDK.
除非另有說明,否則本網站上的文件反映的是 Dart 3.7.1。頁面上次更新於 2025-02-12。 檢視原始碼 或 回報問題。