Flutter目前处于快速演进中,对新手来说的话坑非常多,有必要全面总结。本文针对Windows环境,存量项目的使用。
从官网(https://flutter.dev/docs/development/tools/sdk/releases#windows)下载SDK,在国内的话配置镜像的环境变量:
1 2 |
PUB_HOSTED_URL=https://pub.flutter-io.cn FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn |
将Flutter的bin路径增加到PATH中,例如:
1 |
E:\flutter_windows_1.17.4-stable\flutter\bin |
使用flutter console运行flutter doctor:
把所有问题解决:
注意,Flutter中包含了Dart SDK,我们无需、也不能替换Flutter安装路径里面的Dart SDK。
对于前文开始时的错误而言,其原因在于Android Studio中没有安装Flutter和Dart插件,我们只需要将其安装上,并配置Flutter SDK和Dart SDK的路径即可:
如果在国内的话,需要修改Flutter SDK目录中的flutter.gradle文件,路径形如:
1 |
E:\flutter_windows_1.17.4-stable\flutter\packages\flutter_tools\gradle\flutter.gradle |
将其中的:
1 |
private static final String MAVEN_REPO = "https://storage.proxy.ustclug.org/download.flutter.io"; |
修改为:
1 |
private static final String MAVEN_REPO = "https://storage.flutter-io.cn/download.flutter.io"; |
然后将其中的:
1 2 |
google() jcenter() |
修改为:
1 2 3 4 |
maven { url 'https://maven.aliyun.com/repository/google' } maven { url 'https://maven.aliyun.com/repository/jcenter' } maven { url 'http://maven.aliyun.com/nexus/content/groups/public' } maven { url 'http://download.flutter.io' } |
以此来解决各种各样的网络问题,否则会报诸如下面这些各种报诡异的错误(请注意,下面的这个地址搭梯子是可以访问到的,但是Flutter中有时死活不行,怀疑与超时时间有关,不如直接用镜像):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
E:\>flutter run Launching lib\main.dart on HLK AL00 in debug mode... [!] Your app isn't using AndroidX. To avoid potential build failures, you can quickly migrate your app by following the steps on https://goo.gl/CP92wY. Running Gradle task 'assembleDebug'... FAILURE: Build failed with an exception. * What went wrong: Could not resolve all files for configuration ':app:debugCompileClasspath'. > Could not resolve io.flutter:arm64_v8a_debug:1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7. Required by: project :app > Could not resolve io.flutter:arm64_v8a_debug:1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7. > Could not get resource 'https://storage.proxy.ustclug.org/download.flutter.io/io/flutter/arm64_v8a_debug/1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7/arm64_v8a_debug-1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7.pom'. > Could not HEAD 'https://storage.proxy.ustclug.org/download.flutter.io/io/flutter/arm64_v8a_debug/1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7/arm64_v8a_debug-1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7.pom'. > Connect to storage.proxy.ustclug.org:443 [storage.proxy.ustclug.org/34.64.4.112, storage.proxy.ustclug.org/34.64.4.16, storage.proxy.ustclug.org/34.64.4.80] failed: Connection timed out: connect > Could not resolve io.flutter:arm64_v8a_debug:1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7. > Could not get resource 'https://storage.proxy.ustclug.org/download.flutter.io/io/flutter/arm64_v8a_debug/1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7/arm64_v8a_debug-1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7.pom'. > Could not HEAD 'https://storage.proxy.ustclug.org/download.flutter.io/io/flutter/arm64_v8a_debug/1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7/arm64_v8a_debug-1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7.pom'. > Connect to storage.proxy.ustclug.org:443 [storage.proxy.ustclug.org/34.64.4.112, storage.proxy.ustclug.org/34.64.4.16, storage.proxy.ustclug.org/34.64.4.80] failed: Connection timed out: connect > Skipped due to earlier error > Could not resolve io.flutter:x86_debug:1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7. Required by: project :app > Could not resolve io.flutter:x86_debug:1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7. > Could not get resource 'https://storage.proxy.ustclug.org/download.flutter.io/io/flutter/x86_debug/1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7/x86_debug-1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7.pom'. > Could not HEAD 'https://storage.proxy.ustclug.org/download.flutter.io/io/flutter/x86_debug/1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7/x86_debug-1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7.pom'. > Connect to storage.proxy.ustclug.org:443 [storage.proxy.ustclug.org/34.64.4.112, storage.proxy.ustclug.org/34.64.4.16, storage.proxy.ustclug.org/34.64.4.80] failed: Read timed out > Could not resolve io.flutter:x86_64_debug:1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7. Required by: project :app > Could not resolve io.flutter:x86_64_debug:1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7. > Could not get resource 'https://storage.proxy.ustclug.org/download.flutter.io/io/flutter/x86_64_debug/1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7/x86_64_debug-1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7.pom'. > Could not HEAD 'https://storage.proxy.ustclug.org/download.flutter.io/io/flutter/x86_64_debug/1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7/x86_64_debug-1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7.pom'. > Connect to storage.proxy.ustclug.org:443 [storage.proxy.ustclug.org/34.64.4.112, storage.proxy.ustclug.org/34.64.4.16, storage.proxy.ustclug.org/34.64.4.80] failed: Connection timed out: connect > Could not resolve io.flutter:x86_64_debug:1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7. > Could not get resource 'https://storage.proxy.ustclug.org/download.flutter.io/io/flutter/x86_64_debug/1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7/x86_64_debug-1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7.pom'. > Could not HEAD 'https://storage.proxy.ustclug.org/download.flutter.io/io/flutter/x86_64_debug/1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7/x86_64_debug-1.0.0-ee76268252c22f5c11e82a7b87423ca3982e51a7.pom'. > Connect to storage.proxy.ustclug.org:443 [storage.proxy.ustclug.org/34.64.4.112, storage.proxy.ustclug.org/34.64.4.16, storage.proxy.ustclug.org/34.64.4.80] failed: Connection timed out: connect > Skipped due to earlier error * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 2m 38s |
修改项目中android/build.gradle文件:
对于已有项目而言,在项目根目录运行:
1 2 3 4 5 6 |
flutter clean flutter packages get flutter run flutter build apk flutter build apk --debug flutter build apk --release |
基本上就可以正常使用了。其他常用的命令还有:
1 2 3 |
flutter pub cache repair flutter pub get flutter build apk --verbose |
转载时请保留出处,违法转载追究到底:进城务工人员小梅 » Flutter速成(一)环境搭建及存量项目的使用