Android Studio导入Flutter项目、解决旧Flutter项目不支持空安全所出现的一些问题
下载Flutter SDK:https://storage.googleapis.com/dart-archive/channels/stable/release/2.19.3/sdk/dartsdk-windows-x64-release.zip
解压到你想要放的目录。配置Flutter SDK的环境变量,配置flutter/bin这个目录
settings -> marketplace -> plugins -> 搜索Flutter插件 ->安装Flutter插件,会提示安装Dart插件,点击确定都安装。
Project Sctucture 设置Java的SDK版本为Java 11
-
win + R打开cmd -> 运行命令: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.
-
运行命令:
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就不管了,因为用的时Android Studio开发。
Settings -> Flutter -> 配置Flutter SDK的路径
Settings -> Dart -> 配置Dart SDK路径,一般配置好Flutter SDK,Dart SDK也会有了。-
导入项目,提示:
'Pub get' has not been run,找到pubspec.yaml,点击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"
-
继续'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
上面这些提示其实把版本提上去就好了,提到支持空安全的版本就行。
也可以用命令更新:
dart pub upgrade --null-safety
dart pub get
查看Flutter插件库版本的网站,可以在这里查找插件:
https://pub.dev/
其实高版本的Flutter到这里项目应该就能跑起来了。
一顿操作猛如虎,结果项目太老旧了,用到的很多插件都已经不维护了,没有支持空安全的版本。没办法,只能想其它办法:
百度搜出来的办法:
-
运行/编译项目时加参数禁用空安全
flutter run --no-sound-null-safety flutter build --no-sound-null-safety
-
修改pubspec.yaml中sdk的版本号
environment: # sdk: ">=2.12.0 <3.0.0" // 2.12支持空安全 sdk: ">=2.11.0 <3.0.0" // 2.11.0不支持
一波操作之后也是不行。
最后:降级flutter版本,下载Flutter1.22.4,因为这个版本的Dart版本是 2.10.4,小于2.12。
降级之后,配置Android SDK环境变量:
ANDROID_HOME:C:\Users\Admin\AppData\Local\Android\Sdk
Path配置
%ANDROID_HOME%\emulator
%ANDROID_HOME%\platform-tools
%ANDROID_HOME%\tools
再使用flutter doctor检查配置环境,一顿操作,改了一些问题之后项目终跑起来了。
Flutter学习的一些网站:
- Flutter官方文档:https://flutter.dev/docs
- Flutter中文网:https://flutterchina.club/
- Flutter实战:https://book.flutterchina.club/