在 Flutter 开发中遇到的一些 BUG,避免遗忘,记录一下,如果正在看文章的你也遇到了,那激动的心,颤抖的手,咱们可以握个手。
1.Suggestion:use a compatible library with a minSdk of at most 16,
or increase this project's minSdk version to at least 19,
解决方案
将minsdk提升到19以上
2.flutter打包出来界面空白
这个问题,困扰我两三天了,网上找了千万种方法,无非以下三种情况:
① 升级到flutter SDK stable版本
② flutter clean 并且 flutter build ios --release,最后archive
③更换bundleId
但是都没有卵用
最终我的原因是因为这个界面log有个如下的输出error信息
Incorrect use of ParentDataWidget.
出现这个错误的原因主要是因为Expanded嵌套Column出错了,如下面截图所示:
解决掉这个错误,打包白屏问题即解决,困扰了两天的问题终于解决了。
Incorrect use of ParentDataWidget
3.
Looking up a deactivated widget's ancestor is unsafe.
这个错误的原因主要是因为当前的widget的context是null
StyleConfig styleConfig = Provider.of<StyleConfig>(context);
当前界面如果有类似(context)代码,好好检查下(context)是否为空。
4.
Failed assertion: line 826 pos 14: 'file != null': is not true.
这个问题,我是在拍照选择的时候出现的,模拟器中点击拍照功能,因为模拟器没有摄像头,所以报错,主要原因还是因为代码不严谨的缘故,造成image == null.
原代码为:
Future getImage(bool isTakePhoto) async {
Navigator.pop(context);
var image = await ImagePicker.pickImage(
source:isTakePhoto ? ImageSource.camera :ImageSource.gallery);
setState(() {
// _image = image;
_images.add(image);
});
}
修改后代码为:
Navigator.pop(context);
try {
var image = await ImagePicker.pickImage(
source:isTakePhoto ? ImageSource.camera :ImageSource.gallery
);
if (image == null) {
return;
} else {
setState(() {
// _image = image;
_images.add(image);
});
}
} catch (e) {
print("模拟器不支持相机!");
}
}
5.
The getter 'id' was called on null.
Receiver: null
Tried calling: id
这个错误,主要是因为模型为空,然后取值模型中的字段造成的,或者是因为字段类型不匹配,需要仔细检查,别无它发。
UserModel userModel = Provider.of<UserModel>(context, >listen: false); print("用户ID: ${userModel.user.id}");
比如说这种情况,如果
userModel
是null
,后面使用了userModel.user.id则会报这种错误。
6.
Row's children must not contain any null values, but a null value was found at index 0
这种错误一般是因为Row里面为空造成的,比如项目开发中以下代码就出现过相同的错误:
上面虽然调用了_buildBottomItem方法,但是这个方法内部并没有返回一个widget,所以就报了上述的错误。
修改方法很简单,直接在_buildBottomItem方法中return Container();即可解决。//解决方法: Widget _buildBottomItem(BottomItemType type) { // 写自己的业务逻辑,或return一个默认的Container() return Container(); }
7.
Failed assertion: line 110 pos 12: '_positions.isNotEmpty'
如下图所示:
项目中我的主要是因为在SingleChildScrollView里面嵌套了Swiper,,进行swiper count 判断,如果数量为空则不显示,数量不为空在显示 if (null == _swipers || _swipers.isEmpty)
我的解决方案如下
8.
Looking up a deactivated widget's ancestor is unsafe.
该问题是点击返回的时候报错,意思是不是安全的引用小部件,就是已经销毁的界面然后重复销毁,会爆如下错误,错误信息如下:
根据控制台的错误信息,可以定位到是dispose方法报错了,将FocusScope.of(context).requestFocus(blankFocus);
注释掉即可解决。
9.
RangeError (index): Invalid value: Only valid value is 0: 1
这个报错主要是因为在创建ListView视图时,漏写itemCount,或者itemCount==null造成的。
10.
The getter 'length' was called on null.
Receiver: null
Tried calling: length
这个报错主要是因为某个字段为空造成的,可能数组空,可能走个字段空,都会引起该问题,一定要仔细排查每个字段,别无他法,同时代码写的要健壮一些,不要碰到null就直接抛错误了。例:项目中遇到的报错情况如下:
所以代码需要改为如下,即可消除掉这个报错。
child:Text(formatTime(widget.question.time??""),
style: TextStyle(fontSize: 11,color: infoColor,),),
11.
Failed assertion: line 236 pos 16: 'controller != null': is not true.
这个bug是因为用了SmartRefresher控件,但是并没有写controller造成的,如下列所示的错误写法:
return SmartRefresher(
// controller:_refreshController,
enableTwoLevel:false ,
enablePullDown: true,
header: WaterDropHeader(),
footer: null,
onRefresh: () async {
await pageModel.refresh();
pageModel.showErrorMessage(context);
},
onLoading: null,
enablePullUp: false,
child: Container(),
);
正确的写法应该是必须有 controller: _refreshController.
12.
BUILD FAILED in 36s Exception: Gradle task assembleDebug failed with exit code 1.
详细错误信息如下:
Launching lib/main.dart on V1818CT in debug mode...
Running Gradle task 'assembleDebug'...
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring root project 'android'.
> Could not resolve all artifacts for configuration ':classpath'.
> Could not resolve com.mob.sdk:MobSDK:+.
Required by:
project :
> Failed to list versions for com.mob.sdk:MobSDK.
> Unable to load Maven meta-data from https://maven.aliyun.com/repository/public/com/mob/sdk/MobSDK/maven-metadata.xml.
> Could not get resource 'https://maven.aliyun.com/repository/public/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Could not GET 'https://maven.aliyun.com/repository/public/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Connect to 127.0.0.1:11085 [/127.0.0.1] failed: Connection refused (Connection refused)
> Failed to list versions for com.mob.sdk:MobSDK.
> Unable to load Maven meta-data from https://maven.aliyun.com/repository/jcenter/com/mob/sdk/MobSDK/maven-metadata.xml.
> Could not get resource 'https://maven.aliyun.com/repository/jcenter/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Could not GET 'https://maven.aliyun.com/repository/jcenter/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Connect to 127.0.0.1:11085 [/127.0.0.1] failed: Connection refused (Connection refused)
> Failed to list versions for com.mob.sdk:MobSDK.
> Unable to load Maven meta-data from http://maven.aliyun.com/nexus/content/groups/public/com/mob/sdk/MobSDK/maven-metadata.xml.
> Could not get resource 'http://maven.aliyun.com/nexus/content/groups/public/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Could not GET 'http://maven.aliyun.com/nexus/content/groups/public/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Connect to 127.0.0.1:11085 [/127.0.0.1] failed: Connection refused (Connection refused)
> Failed to list versions for com.mob.sdk:MobSDK.
> Unable to load Maven meta-data from http://mvn.mob.com/android/com/mob/sdk/MobSDK/maven-metadata.xml.
> Could not get resource 'http://mvn.mob.com/android/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Could not GET 'http://mvn.mob.com/android/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Connect to 127.0.0.1:11085 [/127.0.0.1] failed: Connection refused (Connection refused)
> Failed to list versions for com.mob.sdk:MobSDK.
> Unable to load Maven meta-data from https://maven.aliyun.com/repository/google/com/mob/sdk/MobSDK/maven-metadata.xml.
> Could not get resource 'https://maven.aliyun.com/repository/google/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Could not GET 'https://maven.aliyun.com/repository/google/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Connect to 127.0.0.1:11085 [/127.0.0.1] failed: Connection refused (Connection refused)
> Failed to list versions for com.mob.sdk:MobSDK.
> Unable to load Maven meta-data from https://maven.aliyun.com/repository/central/com/mob/sdk/MobSDK/maven-metadata.xml.
> Could not get resource 'https://maven.aliyun.com/repository/central/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Could not GET 'https://maven.aliyun.com/repository/central/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Connect to 127.0.0.1:11085 [/127.0.0.1] failed: Connection refused (Connection refused)
> Failed to list versions for com.mob.sdk:MobSDK.
> Unable to load Maven meta-data from http://download.flutter.io/com/mob/sdk/MobSDK/maven-metadata.xml.
> Could not get resource 'http://download.flutter.io/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Could not GET 'http://download.flutter.io/com/mob/sdk/MobSDK/maven-metadata.xml'.
> Connect to 127.0.0.1:11085 [/127.0.0.1] failed: Connection refused (Connection refused)
* 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 36s
Exception: Gradle task assembleDebug failed with exit code 1
13. fatal:could not username for 'xxxxxx'
这个问题不是flutter代码的问题,最主要原因可能是本地缓存的Git库的账号名和密码账号有问题,参考之前 HTTP Basic: Access denied fatal: Authentication failed...文章的处理方案即可解决。
14.versions is 9.0 to 14.0.99. (in target 'UMCCommon' from project 'Pods')
warning: The iOS Simulator deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 8.0, but the range of supported deployment target versions is 9.0 to 14.0.99. (in target 'mob_sharesdk' from project 'Pods')Could not build the application for the simulator.
Error launching application on iPhone 11 Pro Max.方案一:
post_install do |installer| installer.pods_project.targets.each do |target| flutter_additional_ios_build_settings(target) end end
Podfile文件中将这段代码,替换为下面这段:
post_install do |installer| installer.pods_project.targets.each do |target| target.build_configurations.each do |config| >config.build_settings['IPHONEOS_DEPLOYMENT_T>ARGET'] = '9.0' end flutter_additional_ios_build_settings(target) end end
方案二
将Build Settings最后一行的VALID_ARCHS这一行删除,以为这里么没有支持模拟器。
15.
Could not build the application for the simulator..
Error launching application on iPhone 11 Pro Max..
网上查阅很多资料,包括Stack Overflow说的都是clean,方案有多种,如下:
网上方案一:
Please follow these steps/run commands
flutter clean (in terminal)
flutter build (in terminal)
In Xcode, then clean project
In Xcode, then build project网上方案二
1- Open xcode for the iOS project by clicking on Runner.xcworkspacefile located in iOS directory
2- Click Runner (on the left of the Xcode)
3- Click on Build Settings tab (in the middle of Xcode)
4- Change iOS Deployment Target to 12.1 for example
5- Save your action
6- Runflutter clean
then run your app网上方案三:
rm iOS/Podfile
Then upgrade your packages:
pub upgrade
pub run
And update your podfile:
cd ios && pod update
Then clean and run:
flutter clean && flutter run网上方案四:
解决方法在as的Terminal里面
1.保证在下面根目录下执行下面: flutter clean
2.然后cd到ios目录执行下面: cd iOS
3.最后执行这一步: pod install
4.运行ios模拟器网上方案五:
1、关闭Xcode,打开终端:
2、进入DerivedData目录
cd ~/Library/Developer/Xcode/DerivedData/
3、然后再终端执行:
xattr -rc .
4、再运行flutter项目,完美解决但是网上方案千千万,没有一个解决了我的问题,最后我的方案是:
将本地所有的库全部删除,重新pub get新的库,即可解决问题,这个可能是库缓存错乱了,出现的错误。
解决步骤如下:
至此,这个问题成功解决,耗费了我两天的时长,都是泪~😭