<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] >譯者注:流程控制在大部分語言中大同小異,在Dart中只有最后一個assert比較特殊,其實assert在前邊的章節中也使用過了,如果有其他計算機語言經驗的開發者可以直接跳到assert部分去看。 > *** 您可以使用以下任何一種方法來控制Dart代碼的流: * if 和 else * for循環 * while和do-while循環 * break和continue * switch和case * assert 你也可以使用try-catch和throw來對控制流程作出改變,如[Exception(異常)]中所說明。 ### if 和 else Dart支持帶有可選else語句的if語句,如下面的示例所示。請參閱[條件表達式]。 ~~~ if (isRaining()) { you.bringRainCoat(); } else if (isSnowing()) { you.wearJacket(); } else { car.putTopDown(); } ~~~ 與JavaScript不同的是,條件必須使用布爾值,不允許其他值。有關更多信息,請參見[布爾類型]。 >譯者注:以上說明可能不是非常清晰,那我們通過代碼示例來說明下。我們知道JavaScript等其他很多語言中0和負數可以表示false而正數表示true但是這在Dart中是行不通的,請看下邊的例子: > ~~~ void main() { var a=1; if(a) { print('yes'); }else{ print('error'); } } ~~~ 以上代碼看著貌似沒任何問題,理想的結果輸出為"yes",但是讓我們來看看真實的輸出結果: ![](https://box.kancloud.cn/baa9bffe0c10f33a687b59f2c4b5498a_2648x344.png) 在執行執行時拋出了異常,異常的意思是int類型不能復制給bool類型。從這兒我們也可以看出Dart中if判斷的原理。 *** ### For循環 你可以使用標準for循環進行迭代操作(處理重復的操作),例如: ~~~ var message = StringBuffer('Dart is fun'); for (var i = 0; i < 5; i++) { message.write('!'); } ~~~ Dart for循環內部的閉包捕獲了索引的值,避免了JavaScript中常見的陷阱。例如: ~~~ var callbacks = []; for (var i = 0; i < 2; i++) { callbacks.add(() => print(i)); } callbacks.forEach((c) => c()); ~~~ 輸出是0,然后是1,正如預期的那樣。相反,該示例將在JavaScript中輸出2和2。 >譯者注:當然如果是JavaScript程序猿應該知曉這個問題,下邊我在Chrome的控制臺下演示本錯誤。 > ![](https://box.kancloud.cn/8a2e29ff61684d00f66d45311a422430_429x198.png) 從以上可以看出不同之處。 *** 如果要迭代的對象是可迭代的,那么可以使用forEach()方法。使用forEach()本身是沒有迭代計數器的,也就是for循環本身有i值在forEach()本身是沒有的。 ~~~ candidates.forEach((candidate) => candidate.interview()); ~~~ 可迭代類如List和Set也支持for-in形式的迭代: ~~~ var collection = [0, 1, 2]; for (var x in collection) { print(x); // 0 1 2 } ~~~ ### While 和 do-while 循環 while循環在循環之前先檢驗條件是否為真,例如: ~~~ while (!isDone()) { doSomething(); } ~~~ do-while循環在一次循環結束之后檢查條件是否為真,例如: ~~~ do { printLine(); } while (!atEndOfPage()); ~~~ >譯者注:所以使用while和do-while要非常小心,因為使用do-while如果條件為假也會至少執行一次循環體中的語句。 > *** ### Break 和 continue 使用break終止循環: ~~~ while (true) { if (shutDownRequested()) break; processIncomingRequests(); } ~~~ 使用continue跳出本次循環繼續下次循環: ~~~ for (int i = 0; i < candidates.length; i++) { var candidate = candidates[i]; if (candidate.yearsExperience < 5) { continue; } candidate.interview(); } ~~~ 如果你使用的是可迭代的,比如列表或集合,你可能會用不同的方式來寫這個例子: ~~~ candidates .where((c) => c.yearsExperience >= 5) .forEach((c) => c.interview()); ~~~ >譯者注:對于以上的最后一種寫法想必大家還是感覺很難理解,那么我通過示例代碼給大家說明下: > ~~~ class A { var yearsExperience; A(int year) { this.yearsExperience = year; } interview() { print(this.yearsExperience); } } void main() { List personList = [ A(1), A(2), A(5), A(6) ]; ///第一種寫法 for(int i = 0; i < personList.length; i++) { var person = personList[i]; if (person.yearsExperience < 5) { continue; } person.interview(); } ///第二種寫法 personList .where((c) => c.yearsExperience >= 5) .forEach((c) => c.interview()); } ///執行結果 5 6 5 6 ~~~ 兩次結果都相同,那么這兒的where()方法究竟是什么呢,我們在后續會接觸到,他是dart.core中的iterable.dart中的方法,他返回復合where()中條件的集合視圖,所以在where()后邊我們可以繼續調用forEach(),當然如果where()返回的集合視圖為空也不會出現錯誤,僅僅是forEach()不執行而已。這兒where()的作用類似于過濾器。 *** ### switch 和 case 在Dart中switch語句使用 “==”運算來比較整數,字符串或者編譯時常量。被比較對象必須都是同一個類的實例(而不是它的任何子類型),并且這個類不能重寫“==”操作。枚舉類型在switch語句是一種非常好的應用場景。 >注意:Dart中的Switch語句適用于有限的情況,例如在解釋器或掃描器中。 > switch的規則是每個非空的case子句以一個break語句結束。結束非空case子句的其他有效方法是continue、throw或return語句。 當沒有case子句匹配時,使用default子句執行代碼: ~~~ var command = 'OPEN'; switch (command) { case 'CLOSED': executeClosed(); break; case 'PENDING': executePending(); break; case 'APPROVED': executeApproved(); break; case 'DENIED': executeDenied(); break; case 'OPEN': executeOpen(); break; default: executeUnknown(); } ~~~ 下面的示例省略了case子句中的break語句,從而產生錯誤: ~~~ var command = 'OPEN'; switch (command) { case 'OPEN': executeOpen(); // ERROR: Missing break case 'CLOSED': executeClosed(); break; } ~~~ 然而,Dart支持空的case子句,支持fall-through的格式: ~~~ var command = 'CLOSED'; switch (command) { case 'CLOSED': // Empty case falls through. case 'NOW_CLOSED': // 無論command是CLOSED還是NOW_CLOSED都執行 executeNowClosed(); break; } ~~~ 如果你真的需要使用fall-through格式,你可以使用continue語句和一個標簽,例如: ~~~ var command = 'CLOSED'; switch (command) { case 'CLOSED': executeClosed(); continue nowClosed; // Continues executing at the nowClosed label. nowClosed: case 'NOW_CLOSED': // Runs for both CLOSED and NOW_CLOSED. executeNowClosed(); break; } ~~~ case子句可以有局部變量,只能在該子句的范圍內可見。 ### Assert (斷言) 如果布爾條件為false,則使用assert語句中斷正常執行。您可以在本教程中找到assert聲明的示例。如下示例: ~~~ // Make sure the variable has a non-null value. assert(text != null); // Make sure the value is less than 100. assert(number < 100); // Make sure this is an https URL. assert(urlString.startsWith('https')); ~~~ >注意:Assert語句不會影響生產環境中代碼的執行,它僅僅在測試環境中起作用。在Flutter的調試模式下可以使用assert。默認情況下,像(dartdevc typically)只支持開發環境的工具默認支持assert。例如dart和dart2js通過命令行標記:--enable-asserts來支持asserts。 > 要將提示消息附加到斷言,請添加一個字符串作為第二個參數。 ~~~ assert(urlString.startsWith('https'), 'URL ($urlString) should start with "https".'); ~~~ 斷言的第一個參數可以是任何解析為布爾值的表達式。如果表達式的值為true,則斷言成功并繼續執行。如果是false,則斷言失敗,并拋出異常(AssertionError)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看