創建簡單APP,主要是main.dart
import 'package:flutter/material.dart'; /*void main() { runApp(MyApp()); }*/ void main() => runApp(MyApp()); class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( theme: ThemeData( // This is the theme of your application. // // Try running your application with "flutter run". You'll see the // application has a blue toolbar. Then, without quitting the app, try // changing the primarySwatch below to Colors.green and then invoke // "hot reload" (press "r" in the console where you ran "flutter run", // or simply save your changes to "hot reload" in a Flutter IDE). // Notice that the counter didn't reset back to zero; the application // is not restarted. primarySwatch: Colors.blue, // This makes the visual density adapt to the platform that you run // the app on. For desktop platforms, the controls will be smaller and // closer together (more dense) than on mobile platforms. visualDensity: VisualDensity.adaptivePlatformDensity, ), home: new Scaffold( appBar: new AppBar( title: new Text("First Flutter Demo"), ), body: new Center( child: new Text("It is body"), ), ), ); } }
- 本示例創建一個Material APP。Material是一種標准的移動端和web端的視覺設計語言。 Flutter提供了一套豐富的Material widgets。
- main函數使用了(
=>
)符號, 這是Dart中單行函數或方法的簡寫
void main() => runApp(MyApp()); 等同於 void main() { runApp(MyApp()); }
-
^
符號意味着你可以使用此插件的最新版本(大於等於當前版本)慎用
優點:能使用最新代碼
缺點:編譯出錯,前一天代碼還能編譯,今天編譯出錯情況
-
下划線前綴標識
final _suggestions = <WordPair>[];
在Dart語言中使用下划線前綴標識符,會強制其變成私有的。
- 賦值操作符
AA ?? "999" ///表示如果 AA 為空,返回999 AA ??= "999" ///表示如果 AA 為空,給 AA 設置成 999 AA ~/999 ///AA 對於 999 整除
- 作用域
Dart
沒有關鍵詞public
、private
等修飾符,_
下橫向直接代表private
,但是有@protected
注解 。
- 類、接口、繼承
Dart 中沒有接口,類都可以作為接口,把某個類當做接口實現時,只需要使用 implements ,然后復寫父類方法即可。 Dart 中支持 mixins ,按照出現順序應該為extends 、 mixins 、implements 。
- 同步異步
Dart
中可通過async
/await
或者Future
定義異步操作,而事實上async
/await
也只是語法糖,最終還是通過編譯器轉為Future
。
Dart 中另外一種異步操作,async*
/yield
或者Stream
可定義Stream
異步,async*
/yield
也只是語法糖,最終還是通過編譯器轉為Stream
。Stream 還支持同步操作。
1)、Stream
中主要有 Stream
、 StreamController
、StreamSink
和 StreamSubscription
四個關鍵對象,大致可以總結為:
-
-
StreamController
:如類名描述,用於整個Stream
過程的控制,提供各類接口用於創建各種事件流。 -
StreamSink
:一般作為事件的入口,提供如add
,addStream
等。 -
Stream
:事件源本身,一般可用於監聽事件或者對事件進行轉換,如listen
、where
。 -
StreamSubscription
:事件訂閱后的對象,表面上用於管理訂閱過等各類操作,如cacenl
、pause
,同時在內部也是事件的中轉關鍵。
-
2)、一般通過 StreamController
創建 Stream
;通過 StreamSink
添加事件;通過 Stream
監聽事件;通過 StreamSubscription
管理訂閱。
3)、Stream
中支持各種變化,比如map
、expand
、where
、take
等操作,同時支持轉換為 Future
。
- Flutter生命周期

-
initState()
表示當前State
將和一個BuildContext
產生關聯,但是此時BuildContext
沒有完全裝載完成,如果你需要在該方法中獲取BuildContext
,可以new Future.delayed(const Duration(seconds: 0, (){//context});
一下。 -
didChangeDependencies()
在initState()
之后調用,當State
對象的依賴關系發生變化時,該方法被調用,初始化時也會調用。 -
deactivate()
當State
被暫時從視圖樹中移除時,會調用這個方法,同時頁面切換時,也會調用。 -
dispose()
Widget 銷毀了,在調用這個方法之前,總會先調用 deactivate()。 -
didUpdateWidge
當widget
狀態發生變化時,被調用。
基本類型
-
var 可以定義變量,如
var tag = "666"
,這和 JS 、 Kotlin 等語言類似,同時 Dart 也算半個動態類型語言,同時支持閉包。 -
Dart
屬於是強類型語言 ,但可以用var
來聲明變量,Dart
會自推導出數據類型,所以var
實際上是編譯期的“語法糖”。dynamic
表示動態類型, 被編譯后,實際是一個object
類型,在編譯期間不進行任何的類型檢查,而是在運行期進行類型檢查。 -
Dart 中 number 類型分為
int
和double
,其中 java 中的 long 對應的也是 Dart 中的 int 類型,Dart 中沒有 float 類型。 -
Dart 下只有 bool 型可以用於 if 等判斷,不同於 JS 這種使用方式是不合法的
var g = "null"; if(g){}
。 -
Dart 中,switch 支持 String 類型。
變量
-
Dart 不需要給變量設置
setter getter
方法, 這和 kotlin 等語言類似。Dart 中所有的基礎類型、類等都繼承 Object ,默認值是 NULL, 自帶 getter 和 setter ,而如果是 final 或者 const 的話,那么它只有一個 getter 方法。 -
Dart 中 final 和 const 表示常量,比如
final name = 'GSY'; const value= 1000000;
同時static const
組合代表了靜態常量,其中 const 的值在編譯期確定,final 的值要到運行時才確定。 -
Dart 下的數值,在作為字符串使用時,是需要顯式指定的。比如:
int i = 0; print("aaaa" + i);
這樣並不支持,需要print("aaaa" + i.toString());
這樣使用,這和 Java 與 JS 存在差異,所以在使用動態類型時,需要注意不要把 number 類型當做 String 使用。 -
Dart 中數組等於列表,所以
var list = [];
和List list = new List()
可以簡單看做一樣。