flutter开发(一)flutter命令行工具
安装
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
升级插件包