Android Studio導(dǎo)入Flutter項(xiàng)目浪蹂、解決舊Flutter項(xiàng)目不支持空安全所出現(xiàn)的一些問題
下載Flutter SDK:https://storage.googleapis.com/dart-archive/channels/stable/release/2.19.3/sdk/dartsdk-windows-x64-release.zip
解壓到你想要放的目錄抵栈。配置Flutter SDK的環(huán)境變量,配置flutter/bin這個(gè)目錄
settings -> marketplace -> plugins -> 搜索Flutter插件 ->安裝Flutter插件坤次,會(huì)提示安裝Dart插件古劲,點(diǎn)擊確定都安裝。
Project Sctucture 設(shè)置Java的SDK版本為Java 11
-
win + R打開cmd -> 運(yùn)行命令:flutter doctor
提示:
Doctor summary (to see all details, run flutter doctor -v): [√] Flutter (Channel stable, 3.7.6, on Microsoft Windows [版本 10.0.22000.1574], locale zh-CN) [X] Windows Version (Unable to confirm if installed Windows version is 10 or greater) [!] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1) X cmdline-tools component is missing Run `path/to/sdkmanager --install "cmdline-tools;latest"` See https://developer.android.com/studio/command-line for more details. X Android license status unknown. Run `flutter doctor --android-licenses` to accept the SDK licenses. See https://flutter.dev/docs/get-started/install/windows#android-setup for more details. [√] Chrome - develop for the web [X] Visual Studio - develop for Windows X Visual Studio not installed; this is necessary for Windows development. Download at https://visualstudio.microsoft.com/downloads/. Please install the "Desktop development with C++" workload, including all of its default components [√] Android Studio (version 2022.1) [√] Connected device (4 available) [√] HTTP Host Availability ! Doctor found issues in 3 categories.
-
運(yùn)行命令:
flutter channel flutter channel master
解決:Windows Version (Unable to confirm if installed Windows version is 10 or greater)
打開Android Studio -> Android SDK -> SDK Tools -> 找到cmdline-tools缰猴,安裝即可产艾。
解決:cmdline-tools component is missing
flutter doctor --android-licenses
解決:Android license status unknown.
Visual Studio - develop for Windows就不管了,因?yàn)橛玫臅r(shí)Android Studio開發(fā)滑绒。
Settings -> Flutter -> 配置Flutter SDK的路徑
Settings -> Dart -> 配置Dart SDK路徑闷堡,一般配置好Flutter SDK,Dart SDK也會(huì)有了疑故。-
導(dǎo)入項(xiàng)目杠览,提示:
'Pub get' has not been run,找到pubspec.yaml纵势,點(diǎn)擊Pub get
提示:
The lower bound of "sdk: '>=2.6.0 <3.0.0'" must be 2.12.0 or higher to enable null safety.
修改:
environment: sdk: ">=2.12.0 <3.0.0"
-
繼續(xù)'Pub get'提示:
Because fun_lab depends on json_serializable >=0.1.0+1 <4.1.0 which doesn't support null safety, version solving failed.
修改:
json_annotation: ^4.0.1 json_serializable: ^4.1.2
上面這些提示其實(shí)把版本提上去就好了倦零,提到支持空安全的版本就行误续。
也可以用命令更新:
dart pub upgrade --null-safety
dart pub get
查看Flutter插件庫版本的網(wǎng)站吨悍,可以在這里查找插件:
https://pub.dev/
其實(shí)高版本的Flutter到這里項(xiàng)目應(yīng)該就能跑起來了扫茅。
一頓操作猛如虎,結(jié)果項(xiàng)目太老舊了育瓜,用到的很多插件都已經(jīng)不維護(hù)了葫隙,沒有支持空安全的版本。沒辦法躏仇,只能想其它辦法:
百度搜出來的辦法:
-
運(yùn)行/編譯項(xiàng)目時(shí)加參數(shù)禁用空安全
flutter run --no-sound-null-safety flutter build --no-sound-null-safety
-
修改pubspec.yaml中sdk的版本號(hào)
environment: # sdk: ">=2.12.0 <3.0.0" // 2.12支持空安全 sdk: ">=2.11.0 <3.0.0" // 2.11.0不支持
一波操作之后也是不行恋脚。
最后:降級(jí)flutter版本,下載Flutter1.22.4焰手,因?yàn)檫@個(gè)版本的Dart版本是 2.10.4糟描,小于2.12。
降級(jí)之后书妻,配置Android SDK環(huán)境變量:
ANDROID_HOME:C:\Users\Admin\AppData\Local\Android\Sdk
Path配置
%ANDROID_HOME%\emulator
%ANDROID_HOME%\platform-tools
%ANDROID_HOME%\tools
再使用flutter doctor檢查配置環(huán)境船响,一頓操作,改了一些問題之后項(xiàng)目終跑起來了躲履。
Flutter學(xué)習(xí)的一些網(wǎng)站:
- Flutter官方文檔:https://flutter.dev/docs
- Flutter中文網(wǎng):https://flutterchina.club/
- Flutter實(shí)戰(zhàn):https://book.flutterchina.club/