安裝
Linux下面的flutter安裝比較簡單,在flutter 中文戰 上下載一個最新穩定的版本,解壓到系統上就行了。
我下載的是Linux下的3.32.7版。
解壓之后,flutter目錄里會有bin、dev等目錄,把bin目錄加到系統的PATH環境變量里,就能使用flutter命令了。
配置
根據文檔,在大陸地區使用flutter,最好單獨設置一下鏡像。
我追加了這樣幾行,在$HOME/.bashrc
里:
export PATH=$HOME/flutter/bin:$PATH
export FLUTTER_STORAGE_BASE_URL="https://storage.flutter-io.cn"
export PUB_HOSTED_URL="https://pub.flutter-io.cn"
創建項目
我們可以在一個目錄里執行flutter create
命令,初始化一個項目。
如:
> flutter create myapp
之后就可以進入myapp目錄進行開發了。
flutter create
命令還能修正初始化,即:當我們的項目中自動生成的文件,出現不一致的時候,可以在項目里執行flutter create .
來增量初始化。
我改了項目名稱之后,怎么編譯都出錯,就把android目錄刪掉,重新執行了flutter create .
,就一切正常了。
編譯
使用flutter編譯的命令是build,后面要跟平臺版本。
如,我們在Fedora 42上的項目目錄里執行flutter build
,將會返回:
Build an executable app or install bundle.Global options:
-h, --help Print this usage information.
-v, --verbose Noisy logging, including all shell commands executed.If used with "--help", shows hidden options. If used with "flutter doctor", shows additional diagnosticinformation. (Use "-vv" to force verbose logging in those cases.)
-d, --device-id Target device id or name (prefixes allowed).--version Reports the version of this tool.--enable-analytics Enable telemetry reporting each time a flutter or dart command runs.--disable-analytics Disable telemetry reporting each time a flutter or dart command runs, until it is re-enabled.--suppress-analytics Suppress analytics reporting for the current CLI invocation.Usage: flutter build <subcommand> [arguments]
-h, --help Print this usage information.Available subcommands:aar Build a repository containing an AAR and a POM file.apk Build an Android APK file from your app.appbundle Build an Android App Bundle file from your app.bundle Build the Flutter assets directory from your app.linux Build a Linux desktop application.web Build a web application bundle.Run "flutter help" to see global options.
通過最后部分,我們知道可以編譯的版本有:aar、apk、appbundle、bundle、linux與web。
如:flutter build apk
將編譯一個Android上面的apk包出來。
分析
如果要分析源代碼是否有問題,可以使用:
> flutter analyze
運行
如果要運行程序,可以使用flutter run
命令。
默認地,flutter將啟動第一個可用的設備。
> flutter run
也可以加上-d ID
設備id的方式,來啟動程序到指定的設備上。
> flutter run -d linux
Android虛擬機
kvm模塊
要使用Android虛擬機,需要加載kvm驅動模塊。
而且,Intel與AMD的kvm模塊不一樣。
在Intel 的CPU系統中,使用modprobe kvm-intel
;在AMD的CPU系統中,使用modprobe kvm-amd
。
枚舉Android設備
枚舉系統上可以用的Android設備,使用flutter emulators
。
> flutter emulators
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
2 available emulators: Id ? Name ? Manufacturer ? Platform Medium_Phone_API_35 ? Medium Phone API 35 ? Generic ? android
Resizable_Experimental_API_34 ? Resizable (Experimental) API 34 ? Generic ? android To run an emulator, run 'flutter emulators --launch <emulator id>'.
To create a new emulator, run 'flutter emulators --create [--name xyz]'. You can find more information on managing emulators at the links below: https://developer.android.com/studio/run/managing-avds https://developer.android.com/studio/command-line/avdmanager
啟動Android設備
flutter emulators
命令加上--launch ID
命令,即啟動了相應ID的設備。
如:
> flutter emulators --launch Medium_Phone_API_35
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
查看運行的Android設備ID
啟動Android設備之后,就可以使用flutter devices
查看到了。
> flutter devices
Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
Found 2 connected devices: sdk gphone16k x86 64 (mobile) ? emulator-5554 ? android-x64 ? Android 15 (API 35) (emulator) Linux (desktop) ? linux ? linux-x64 ? Fedora Linux 42 (Workstation Edition) 6.15.6-200.fc42.x86_64 Run "flutter emulators" to list and start any available device emulators. If you expected another device to be detected, please run "flutter doctor" to diagnose potential issues. You may also try increasing
the time to wait for connected devices with the "--device-timeout" flag. Visit https://flutter.dev/setup/ for troubleshooting tips.
在運行的Android虛擬機運行
通過上面的flutter devices
命令,我們得到了兩個可以使用的運行中的設備,一個是emulator-5554
,一個是linux
。
其中,linux是主機,emulator-5554就是我們剛剛通過flutter emulators --launch ID
啟動起來的安卓虛擬機。
使用flutter run -d ID
命令,就可以在這個虛擬機上運行應用。
> flutter run -d emulator-5554
依賴
flutter pub
加上子命令,可以管理插件包。
其中,
flutter pub add
加入一個插件包flutter pub get
下載插件包flutter pub outdated
檢查過時插件包flutter pub upgrade
升級插件包