Flutter开发技巧:常见问题汇总

646 阅读4分钟

1.版本冲突

 例:And because build_runner >=2.0.0 depends on shelf ^1.0.0, dio ^3.0.10 is incompatible with build_runner >=2.0.0.
So, because xfjyflutter depends on both dio ^3.0.10 and build_runner ^2.0.0, version solving failed.

解决方式: build_runner: anypub get之后 找到pubspec.lock打开文件中当前依赖生成版本,再回到pubspec.yaml文件中设置成所用版本

为什么不直接使用any作为版本号呢 在实际项目开发中,拥有不确定性的版本号会成为未来应用崩溃的一个极大的隐患。这可能将会使您的应用难以调试。所以请记住,不要在你的项目中留下any关键字!

2. web模拟器无法运行提示 To serve from a subpath "foo" (i.e. http://localhost:8080/foo/ instead of http://localhost:8080/) use:

image.png 找到web文件配置中的index.html文件下的改掉它

3.in ios 14+ ,debug mode flutter apps can only be launched form flutter tooling,ides with flutter plugins or from xcode

flutter run --release

或则

更改Xcode编译模式
Xcode使用Release模式编译,这个时候Flutter编译方式为AOT,可正常启动。

4. note: Building targets in dependency order

/Users/cindy/mofang/xfjy-flutter-module/.ios/Pods/Pods.xcodeproj: warning: The iOS deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 8.0, but the range of supported deployment target
versions is 9.0 to 15.5.99. (in target 'FMDB' from project 'Pods')

解决方法: 把 ios 目录下的 Podfile 文件最后的

post_install do |installer|
  installer.pods_project.targets.each do |target|
    flutter_additional_ios_build_settings(target)
  end
end

修改成:

post_install do |installer|
  installer.pods_project.targets.each do |target|
      target.build_configurations.each do |config|
          config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '10.0'
      end
      flutter_additional_ios_build_settings(target)
  end
end

5.遇到项目中报com.android.tools.build:gradle:3.4.0

flutter项目引用第三方包,或者新同事从svn下载的项目,运行报错,提示找不到 ‘com.android.tools.build:gradle:3.4.0’,解决办法 找到项目的根目录下android/gradle.properties文件新增

systemProp.https.proxyPort=443
systemProp.http.proxyPort=443

两行 后面的端口可能不一样,可根据报错信息来修改。 运行起来这两行也可以注释掉了,因为已经下载好了

6.Flutter 打包命令

  • 打包aar(与原生混合项目,一般打aar给原生项目使用)

flutter build aar --no-tree-shake-icons

  • 打包apk(纯Flutter项目使用)

flutter build apk --no-tree-shake-icons

7. Unable to find bundled Java version =>Mac Flutter问题 Android Studio Arctic Fox 2020.3.1

Unable to find bundled Java version.
    • Try updating or re-installing Android Studio.

处理方式:
    cd /Applications/Android\ Studio.app/Contents/jre
    ln -s ../jre jdk
    ln -s "/Library/Internet Plug-Ins/JavaAppletPlugin.plugin" jdk

8. No cached version available for offline mode 错误

导入 gradle 项目到 IDEA,jar 包依赖一直有问题。在 IDEA 里面鼓捣了半天 gradle 配置,但是下载依赖的 jar 包时冒出了一个 No cached version available for offline mode 错误,如下图所示 image.png

提示的信息很清楚,无法下载 jar 包,本地也没有缓存的 jar 包供离线模式使用。网上搜了下,开启了离线模式的话依赖管理工具就不会去下载 jar 包,只使用本地仓库缓存的 jar 包,本地没有就报出这个错误

在新版本的 IDEA 中,离线模式的配置挪到了依赖插件侧边,笔者配置插件的时候应该是没注意点击了一下,点击下图所示按钮关闭离线模式[Toggle Offline Mode]即可

image.png

9.异常INSTALL_PARSE_FAILED_MANIFEST_MALFORMED

第一步:打开AndroidManifest.xml文件,在标签中添加属性

android:exported="true"

image.png

第二步:打开gradle.properties文件,添加属性:

android.injected.testOnly=false

10.does not exist import androidx.lifecycle.Lifecycle

11.安装新版android studio后出现找不到环境变量问题,原因是新版更改了JDK的路径原来在jre文件夹下,现在在jbr文件夹下

cd /Applications/Android\ Studio.app/Contents 
ln -s jbr jre

12. Could not resolve all dependencies for configuration ':classpath'

Could not resolve all dependencies for configuration ':classpath'. Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven repository 'maven4(developer.huawei.com/repo/)' to redirect to a secure protocol (like HTTPS) or allow insecure protocols. See docs.gradle.org/7.4/dsl/org… for more details. A problem occurred configuring project ':flutter'. A problem occurred evaluating project ':flutter'. > Plugin with id 'com.android.library' not found.

13. import androidx.annotation.RequiresApi;

 import androidx.annotation.RequiresApi; 
                         ^
  symbol:   class RequiresApi
  location: package androidx.annotation

image.png

手动添加依赖

dependencies {  
testImplementation 'junit:junit:4.13.2'  
implementation 'androidx.annotation:annotation:1.3.0'  
  
}

14.error: package androidx.lifecycle does not exist import androidx.lifecycle.Lifecycle;

error: package androidx.lifecycle does not exist
import androidx.lifecycle.Lifecycle;

image.png

手动在build.gradle中添加 image.png 代码如下:

dependencies {  
testImplementation 'junit:junit:4.13.2'  
testImplementation 'org.mockito:mockito-core:5.1.1'  
implementation 'androidx.lifecycle:lifecycle-common:2.5.1'  
}

14.Class 'kotlin.Unit' was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.8.0, expected version is 1.6.0.

修改Kotlin_version为报错信息中最高的版本(我这次升级到1.8.0有异常,升级到1.7.2可以用)

  1. 更新Kotlin版本:确保你的项目中使用的Kotlin版本与annotation-jvm库兼容。你可以在项目的构建文件(如build.gradle)中将Kotlin版本更新为1.6.0或更高版本。
  2. 检查依赖关系:确认你的项目依赖项中是否包含annotation-jvm库的正确版本。你可以查看项目的构建文件(如build.gradle)中的依赖项声明,并确保annotation-jvm库的版本与你的项目要求相匹配。