在 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,),),