Flutter - 记录遇到的一些问题

demo 地址: https://github.com/iotjin/jh_flutter_demo
代码不定时更新,请前往github查看最新代码

Flutter - 记录遇到的一些问题

  • 编译
  • 20、web打包后运行报错
  • 19、Flutter报错: 动态TabBar `RangeError (index): Invalid value: Only valid value is 0: 1`
  • 18、打未发布的包后华为安装不上,提示该安装包未包含任何证书
  • 17、Flutter编译警告: Warning: This version only understands SDK XML versions up to 2 but an SDK XML file of version 3 was encountered.
  • 16、Flutter编译报错: android:exported needs to be explicitly specified for .
  • 15、Flutter升级3.0后处理SchedulerBinding警告⚠️ dependency_overrides拉不下来git代码
  • 14、Flutter报错: type ‘double‘ is not a subtype of type ‘int?‘或type ‘int‘ is not a subtype of type ‘double
  • 13、.gitignore文件不生效
  • 12、gradle.properties 中文乱码
  • 11、三方库冲突
  • 10、Gradle 版本和plugin 版本不一致问题
    • 10.1、Gradle版本过低
    • 10.2、Gradle版本过高
    • 10.3、kotlin版本不一致
  • 9、flutter从V1项目升级V2
  • 8、 iOS14debug模式闪退
  • 7、 flutter真机调试一直显示loading
  • 6、 Podfile 过期:
  • 5、 flutter 运行 iOS 报错:
  • 4、 flutter 警告iOS deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 4.3:
  • 3、 flutter ios编译运行失败
  • 2、 MuMu运行安卓闪退
  • 1、 修改包名

编译

夜神:

windows控制台:
cd C:\Program Files (x86)\Nox\bin
nox_adb.exe connect 127.0.0.1:62001

Android Studio的Terminal窗口:

adb connect 127.0.0.1:62001

MuMu:

Android Studio的Terminal窗口:

adb connect 127.0.0.1:7555

卡在Installing build\app\outputs\flutter-apk\app.apk...

adb uninstall com.example.jh_flutter_demo

pubspec.yaml

# 三方库管理
# 网站1:https://pub.flutter-io.cn
# 网站2:https://pub.dev
# dart 2.12 之后需要处理空安全
# ^ 表示 大版本不变,小版本使用最新的版本
# 三方库冲突时使用 any ,执行 flutter pub get命令,然后在pubspec.lock替换版本号
# 三方库未适配空安全,点击main.dart -> Additional arguments 添加 --no-sound-null-safety

20、web打包后运行报错

index.html:46 Uncaught ReferenceError: _flutter is not defined
    at index.html:46

[解决方案]

修改构建结果中的index.html文件中的base标签,修改成你github仓库的名字,不然关联不到相对路径资源文件.

手动修改:

由原来的改成

打包命令自动修改,加上--base-href=/jh_flutter_demo/

flutter build web --web-renderer html --base-href=/jh_flutter_demo/

19、Flutter报错: 动态TabBar RangeError (index): Invalid value: Only valid value is 0: 1

动态设置TabBar时,会报错RangeError (index): Invalid value: Only valid value is 0: 1

The following RangeError was thrown building TabBar(dirty, dependencies: [_LocalizationsScope-[GlobalKey#f7510], Directionality, _InheritedTheme], state: _TabBarState#6fa4b):
RangeError (index): Invalid value: Only valid value is 0: 1

[解决方案]

添加key或者tabs设置的时候转换一下

TabBar(
   tabs: _myTabs, // 报错的
   
   // 方法一
   key: UniqueKey(), 
   // 方法二
	tabs: _myTabs.map<Tab>((Tab tab) {
            return Tab(
                text: tab.text,
              );
            }).toList(),
   controller: _tabController,
),

18、打未发布的包后华为安装不上,提示该安装包未包含任何证书

首页先看看手机安全设置是否允许安装外部应用,和外部来源应用检查
其次代码方面,试试以下几个方法哪个可行

Android应用解析包错误原因的总结(不定时更新)

1、更新Android SDK ,用的版本都下载安装
Flutter - 记录遇到的一些问题_第1张图片
2、android/app/build.gradle文件,添加v1、v2打包

【Android】App安装提示“该安装包未包含任何证书”问题处理

V1(Jar Signature):
验证未解压的文件内容,APK 签署后可进行许多修改 ,可以移动甚至重新压缩文件。
V2(Full APK Signature):
验证压缩文件的所有字节,而不是单个 ZIP 条目,在签名后无法再更改(包括 zipalign),压缩、调整和签署合并成一步完成。V2(Full APK Signature)更安全而且新的签名可缩短在设备上进行验证的时间(不需要费时地解压缩然后验证),从而加快应用安装速度。如有任何自定义任务篡改 APK 文件或对其进行后处理(无论以任何方式)
需要注意:
V2(Full APK Signature)是Android 7.0后才有的,为了更好的兼容,打包时还是把两个对选上较好,这样还能一定程度上避免一定的问题。

    signingConfigs {
        release {
            v1SigningEnabled true  //打包时默认勾选 V1(Jar Signature)
            v2SigningEnabled true  //打包时默认勾选 V2(Full APK Signature)
        }
    }

3、android/gradle.properties文件,添加下面代码
正式版不建议加

android.injected.testOnly=false

4、android/app/src/main/AndroidManifest.xml文件 ,添加权限

uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

17、Flutter编译警告: Warning: This version only understands SDK XML versions up to 2 but an SDK XML file of version 3 was encountered.

Warning: This version only understands SDK XML versions up to 2 but an SDK XML file of version 3 was encountered. This can happen if you use versions of Android Studio and the command-line tools that were released at different times.

[解决方案]
更新最新的SDK,应用即可
Tools-> SDK manager -> Android SDK -> 选择版本) -> apply
Flutter - 记录遇到的一些问题_第2张图片

16、Flutter编译报错: android:exported needs to be explicitly specified for .

在将targetSdkVersion改为31以上(Android 12) 后报错,
因为android:exported需要为显式指定。当相应组件定义了意图过滤器时,针对Android 12及更高版本的应用程序需要为Android:exported指定一个显式值

/xxx项目路径/android/app/src/main/AndroidManifest.xml Error:
	android:exported needs to be explicitly specified for <activity>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:processDebugMainManifest'.
> Manifest merger failed : android:exported needs to be explicitly specified for <activity>. Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.

* 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 4s
Exception: Gradle task assembleDebug failed with exit code 1

[解决方案]

在路径android/app/src/main/AndroidManifest.xml文件中的 中加上
android:exported=“true”

 android:exported="true"

15、Flutter升级3.0后处理SchedulerBinding警告⚠️ dependency_overrides拉不下来git代码

项目中用了azlistview和charts_flutter,3.0后运行控制台会有警告
Warning: Operand of null-aware operation '!' has type 'SchedulerBinding' which excludes null.
可在dependency_overrides中按下面命令重新 Pub get

dependency_overrides:
  # Pub get 没被覆盖,可删除或修复flutterSDk .pub-cache 中的所有依赖项
  # 运行命令:flutter pub cache clean 或 flutter pub cache repair 后删除 bin/cache路径下的lockfile重试
  # 因为引用了GitHub的charts_flutter,要确保能访问GitHub

  # wechat_camera_picker引用,有警告
  camera: ^0.10.0+1

  # 以下两个处理升级3.0后SchedulerBinding引起的警告(binding的instance是不可为空的,所以不需要使用!)

  # azlistview引用 https://pub.flutter-io.cn/packages/scrollable_positioned_list
  # https://github.com/flutterchina/azlistview/issues/81
  scrollable_positioned_list: ^0.3.2
  # add override of charts_flutter
  # https://github.com/google/charts/issues/771
  charts_flutter:
    git:
      url: https://github.com/kennenfromchina/charts.git
      path: charts_flutter
      ref: fix_flutter3.0

14、Flutter报错: type ‘double‘ is not a subtype of type ‘int?‘或type ‘int‘ is not a subtype of type ‘double

model里比如有个price字段,返回的数据有时是int有时是double类型,这样用double接收就会报错,

double? price;

[解决方案]

double换成numdynamic 类型

num? price;
dynamic price;

13、.gitignore文件不生效

gitignore文件只会在第一次提交项目的时候写入缓存,如果你第一次提交项目时候没有.gitignore文件,或者后来更改,再提交代码想忽略某个文件是不生效的。

[解决方案]

使用时最好所有文件已经完全push了

# 清除缓存文件
git rm -r --cached .
git add .
git commit -m 'update .gitignore'
git push

12、gradle.properties 中文乱码

把Settings -》 File Encodings下面两项默认配置改成UTF-8

默认:

Flutter - 记录遇到的一些问题_第3张图片

更改后:

Flutter - 记录遇到的一些问题_第4张图片

顺便附上gradle.properties

android.useAndroidX=true
android.enableJetifier=true

# 处理 Failed to transform bcprov-jdk15on-1.68.jar 
android.jetifier.blacklist=bcprov-jdk15on

# 提升编译速度配置 https://blog.csdn.net/weixin_33943347/article/details/91361727
org.gradle.daemon=true
org.gradle.configureondemand=true
org.gradle.parallel=true
org.gradle.jvmargs=-Xmx3072m -XX:+UseParallelGC -XX:MaxPermSize=1024m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

# 开启gradle缓存
org.gradle.caching=true

#开启 kotlin 的增量和并行编译
kotlin.incremental=true
kotlin.incremental.java=true
kotlin.incremental.js=true
kotlin.caching.enabled=true
kotlin.parallel.tasks.in.project=true

11、三方库冲突

pubspec.yaml引用的三方库升级时有时几个之间会冲突

[解决方案]

把冲突的版本号改为 any
执行 flutter pub get命令
然后在pubspec.lock查找使用的版本号
再替换any

10、Gradle 版本和plugin 版本不一致问题

插件版本和所需的 Gradle 版本要对应
具体Gradle版本对应的插件版本请移步Android Gradle 插件版本说明

gradle插件版本的配置路径
路径:项目名\bulid.gradle
然后将bulid.gradle文件classpath 的值改成对应的版本号

dependencies {
   classpath 'com.android.tools.build:gradle:4.1.0'
}

所需的 Gradle 版本的配置路径
路径:项目名\gradle\wrapper\gradle-wrapper.properties
然后将gradle-wrapper.properties文件distributionUrl的值改成对应的gradle版本号

distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip

新版 Android Studio 的 File > Project Structure > Project 菜单中指定 Gradle 版本

Flutter - 记录遇到的一些问题_第5张图片

注:

使用 Android Gradle 插件 7.0 构建应用时,现在需要 JDK 11 才能运行 Gradle。Android Studio Arctic Fox 捆绑了 JDK 11,并将 Gradle 配置为默认使用 JDK 11,这意味着大多数 Android Studio 用户不需要对项目进行任何配置更改。

10.1、Gradle版本过低

运行报错

> Failed to apply plugin [id 'kotlin-android']
> The current Gradle version 5.6.2 is not compatible with the Kotlin Gradle plugin. Please use Gradle 6.1.1 or newer, or the previous version of the Kotlin plugin.

[解决方案]

distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip

10.2、Gradle版本过高

运行报错

A problem was found with the configuration of task ':app:checkDebugManifest' (type 'CheckManifest').
- Type 'com.android.build.gradle.internal.tasks.CheckManifest' property 'manifest' has @Input annotation used on property of type 'File'.

[解决方案]

将gradle-wrapper.properties中的gradle版本降级

distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip

修改为

distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip

10.3、kotlin版本不一致

运行报错

FAILURE: Build failed with an exception.

* Where:

* What went wrong:
A problem occurred evaluating project ':app'.
> Could not initialize class org.jetbrains.kotlin.gradle.plugin.sources.DefaultKotlinSourceSetKt

* 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 3s
Exception: Gradle task assembleDebug failed with exit code 1

[解决方案]

查看 kotlin 插件版本后指定对应的版本
File > Settings > Plugins,搜索kotlin查看版本号
然后修改bulid.gradle,路径:项目名\bulid.gradle

buildscript {
    ext.kotlin_version = '1.3.50'
    repositories {
        google()
        mavenCentral()
//        maven { url "https://kotlin.bintray.com/kotlinx" }
//        maven { url 'https://maven.aliyun.com/repository/google' }
//        maven { url 'https://maven.aliyun.com/repository/jcenter' }
//        maven { url 'https://maven.aliyun.com/nexus/content/groups/public' }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:4.1.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

Flutter - 记录遇到的一些问题_第6张图片

9、flutter从V1项目升级V2

运行报错

This app is using a deprecated version of the Android embedding.
To avoid unexpected runtime failures, or future build failures, try to migrate this app to the V2 embedding.
Take a look at the docs for migrating an app: 
https://github.com/flutter/flutter/wiki/Upgrading-pre-1.12-Android-projects

[解决方案]
修改\android\app\src\main目录下的AndroidManifest.xml文件

<manifest>
    <application 
    	android:name="${applicationName}">
    
        <meta-data android:name="flutterEmbedding" android:value="2" />
        
    application>
manifest>

8、 iOS14debug模式闪退

重现方式:

运行debug模式的程序,大退之后从桌面图标点击进入App闪退

[两种解决方案]

使用flutter的release模式 flutter run --release
使用flutter的beta版本v1.22

7、 flutter真机调试一直显示loading

image.png

[解决办法]
在安装flutter的路径底下进入bin/cache,然后把下图的lockfile删除,重启编译器
Flutter - 记录遇到的一些问题_第7张图片

6、 Podfile 过期:

Warning: Podfile is out of date
  This can cause a mismatched version of Flutter to be embedded in your app, which may result in App Store submission rejection or crashes.
  If you have local Podfile edits you would like to keep, see https://github.com/flutter/flutter/issues/24641 for instructions.
To regenerate the Podfile, run:
  rm iOS/Podfile
  警告:Podfile已过期
   这可能会导致将不匹配的Flutter版本嵌入到您的应用中,这可能导致App Store提交被拒绝或崩溃。
   如果您要保留本地Podfile编辑,请参阅https://github.com/flutter/flutter/issues/24641了解说明。
要重新生成Podfile,请运行:
   rm ios / Podfile

大多数情况是在升级flutter版本后发生的,找到Podfile文件直接删除,路径项目名称/ios/Podfile

5、 flutter 运行 iOS 报错:

Automatically assigning platform `iOS` with version `8.0` on target `Runner` because no platform was specified. 
Please specify a platform for this target in your Podfile. 
See `https://guides.cocoapods.org/syntax/podfile.html#platform`.

[解决办法]
去掉 #号打开注释

# platform :ios, '9.0'

4、 flutter 警告iOS deployment target ‘IPHONEOS_DEPLOYMENT_TARGET’ is set to 4.3:

warning: The iOS deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 4.3, 
but the range of supported deployment target versions is 8.0 to 13.4.99.
 (in target 'FMDB' from project 'Pods')

[解决办法]
Podfile 添加

 config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '9.0'

Flutter - 记录遇到的一些问题_第8张图片

3、 flutter ios编译运行失败

显示以下提示:

error: Building for iOS, but the linked and embedded framework 'App.framework' was built for iOS Simulator. (in target 'Runner' from project 'Runner')

Could not build the precompiled application for the device.

It appears that your application still contains the default signing identifier.
Try replacing 'com.example' with your signing id in Xcode:
  open iOS/Runner.xcworkspace

[解决办法]
删除 iOS- Flutter - APP.framework 文件夹

Flutter - 记录遇到的一些问题_第9张图片

2、 MuMu运行安卓闪退

[ERROR:flutter/shell/gpu/gpu_surface_gl.cc(70)] Failed to setup Skia Gr context.
Could not update files on device: HttpException: Connection closed before full header was received, uri = http://127.0.0.1:65203/vcd6t35L_rE=/

[解决办法]

解决办法1 终端运行 flutter run --enable-software-rendering
解决办法2,mian 编辑加 --enable-software-rendering

1、 修改包名

Could not find the built application bundle at build/ios/iphonesimulator/Runner.app.
Error launching application on iPhone 11 Pro Max.

错误的原因是在xcode中修改了display Name字段的名字 默认是Runner,将display Name修改为Runner即可 ,通过 bundle Name 修改包名

你可能感兴趣的:(Flutter,flutter,xcode,ios)