yang
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Dart介紹 ## <font color="#f00">內容涵蓋</font> * 語法、資料類型 * 運算子(operator) * 條件 * 迴圈 * 函數 * 物件導向-類別(class)、封裝(encapsulation)、介面(interface)、抽象類別(abstract class)null safety(空值確認) # 語法、變數、資料類型 Flutter使用Dart語言作為開發工具,Dart為Google開發前端物件導向語言,使用者可以利用線上練習工具[DartPad](https://dartpad.dev/?)在線上練習Dart語言。Dart程式語法類似Java,變數大小寫不同,資料類型包含<span class="blue">var(變數宣告)、int(整數)、double(有小數)、String(字串)、bool(布林值)、dynamic(動態)、List(陣列)、Set(集合)、Map(對應)以下僅介紹var(變數宣告)、dynamic(動態)、List(陣列)、Set(集合)、Map(對應)</span>。Dart不允許變數為空值(null),如果使用者在宣告變數允許變數為空值時,必須在資料類型後加上<span class="blue">?d</span>,例如<font color="#f00">int? a</font>; <style> .blue { color: red; } </style> * <font color="#f00">var</font>:變數會依照後面的資料型態自動判斷,可以設定新值,但是資料類型要與原先設定相同。 ```java var a="123"; a="book"; ``` * <font color="#f00">dynamic</font>:動態類型,不同於var資料類型,變數可以設定新值或不同資料類型值。 ```java dynamic a=123; a="book"; ``` * <font color="#f00">List<資料類型></font>:清單類型,清單類型設定分為兩種:<font color="#f00">固定大小與變動大小</font>。固定大小清單在程式執行時不能改變清單大小。 ```java var a=new List<int>.filled(5,0); //產生5個值為0的定大小清單 print(a); var b=<int>[]; //產生變動大小清單,藉由add新增元素 b.add(1); print(b); ``` * <font color="#f00">Set:集合類型</font>,集合值包含於大括符中,集合可使用in運算子判斷元素是否在集合中。 ```java var a=<int>{}; //整數類型集合 a.add(1); print(a); ``` *<font color="#f00">Map(對應)</font>,對應資料類型為一物件定義鍵(key)與值(value)對應。 ```java void main() { var map1={'name':'甲','phone':'1234567'}; print(map1['name']); var map2=new Map(); map2['id']=1; map2['address']='南台街1號'; print(map2); } ``` # 運算子(operators) #### 運算子(operator)用於變數與值的運算,常用運算子包含計算、設定、比較、邏輯、隸屬等,接下來每個類別進行介紹。 * 計算運算子<font color="#f00"> +、-、*、/、%、++、-- </font>,除了加、減、乘、除之外, % 代表求餘數, ++ 代表增加1簡寫,-- 代表減少1簡寫。 * 設定運算子<font color="#f00"> =、+=、-=、*=、/=、%=、??= </font>,用於變數設定表示,以 x+=5為例,他代表 x=x+5,<font color="#f00">??=</font>會檢查變數是否為<font color="#f00">null</font>,如果是會進行設定。 ```java var a; a??=1; print(a); //印出1 var b=10; b??=1; print(b); //印出10 ``` * 比較運算子 ==、!=、>、>=、<、<=,用於條件比較分別為 等於、不等於、大於、大於等於、小於、小於等於。 * 邏輯運算子 <font color="#f00">&&、||、! </font>,用於串接多個條件比較分別為 且、或、否定。 # 條件- if, switch case #### if條件命令包含 <font color="#f00">if、if else、if ... else if ... else</font>,三長分別為一個條件動作、兩個條件動作與多個條件動作。 ```java //if if (1<2) { print("1小於2"); } //if else if (1<2) { print("1小於2"); } else { print("1不小於2"); } //if else if else if (1>2) { print("1大於2"); } else if (1==2) { print("1等於2"); } else { print("1小於2"); } ``` # swicth多條件判斷控制 ```java void main() { int intScore = 5; switch (intScore) { case 5: print("恭喜!獲得滿分五顆星"); break; case 4: print("讚!讚!讚!獲得四顆榮耀星"); break; case 3: print("可惜!再加油~~~"); break; case 2: print("喔喔!相信自己可以的,多多努力"); break; case 1: print("不敢相信你的分數只有一顆星"); break; case 0: print("你真的有認真準備嗎?"); break; default: print("資料異常!!!"); } } ``` # 迴圈 #### 迴圈命令包含 <font color="#f00">while、do while、for、for ... in、forEach</font>,while、do whilef差別 結束條件在一開始或結束條件在最後,for迴圈使用方式 for (資料類型 變數=起始值;變數結束條件設定;變數增加值設定),for (資料類型 變數 in 清單或集合變數),forEach用於清單或集合,清單或集合變數.forEach((變數類型 元素變數){})。 ```java //while印出1到10數字 int i=1; while (i<=10) { print(i); i=i+1; } //do while印出1到10數字 i=1; do { print(i); i=i+1; } while (i<=10); //for印出1到10數字 for (i=1;i<=10;i++) { print(i); } //foreach印出陣列包含1到10元素 var j=[1,2,3,4,5,6,7,8,9,10]; j.forEach((int k) { print(k); }); ``` # 函數 #### 函數定義為<font color="#f00"> 函數傳回資料類型 函數名稱(資料類型 代入變數)</font>,函數可以有許多代入值,如果有傳回值則使用 <font color="#f00">return 傳回值</font>,如果沒有傳回值,函數傳回資料類型請設為,<font color="#f00">void</font>,Dart允許選擇性代入參數,選擇性代入參數會放在非選擇性代入參數之後,用方括號括起,如果沒有代入值,變數值為null。 ``` //函數func1沒有代入值、沒有傳回值並印出Hello void func1(int a,[int? b,int? c]) { print(a); print(b); print(c); } void main() { func1(10); //印出 10 null null } ``` # 物件導向-類別(class)、封裝(encapsulation)、介面(interface)、抽象類別(abstract class) #### 物件導向程式利用物件來解決問題,而<font color="#f00">類別(class)</font>為物件的抽象描述,他描述並定義物件的屬性與方法,使用者可以想像 <font color="#f00">類別為物件的定義</font>,而物件為類別的實現,另外使用者可以想像:類別就像建造房屋的藍圖,而房屋就是藍圖的實現。類別定義包含<font color="#f00">類別名稱、屬性、方法</font>,屬性是用於描述對應類別特性,而方法是定義類別可執行的動作。 ```java import 'dart:math'; class Point { final double x; //屬性 final double y; Point(this.x, this.y); //建構式 double distanceTo(Point other) { //方法 var dx = x - other.x; var dy = y - other.y; return sqrt(dx * dx + dy * dy); } } class Television { void turnOn() { _illuminateDisplay(); _activateIrSensor(); } set contrast(int value) {...} } class SmartTelevision extends Television { //使用extends繼承Television類別 void turnOn() { //方法隱藏並使用super呼叫父類別Television中turnOn方法 super.turnOn(); _bootNetworkInterface(); _initializeMemory(); _upgradeApps(); } @override set contrast(num value) {...} //覆寫父類別Televison屬性contrast設定 } abstract class Doer { //定義抽象類別 void doSomething(); } class EffectiveDoer extends Doer { //繼承抽象類別並實作方法 void doSomething() { } } ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully