Flutter开发 - 开发中遇到的异常信息汇总,附原因和解决方法

1,175 阅读2分钟
1.Error: Could not find the correct Provider<xxxxxxxxx> above this xxxxxx Widget 
这个不是找不到provider,而是找到的provider不正确,那么应该是context传的不对

2.NoSuchMethodError: The method '[]' was called on null. 
方法返回的是一个数组,但是没有做为空的判断,这个尤其要注意,不止是数组,还有其他的类型

3.NoSuchMethodError: The method 'markNeedsBuild' was called on null.
情况一:在页面出现之前进行了UI的刷新,加个delay0的方法即可解决
情况二:发送验证码的时候用到定时器来刷新倒计时,党页面销毁的时候,定时器还在工作,就会导致这个异常

4.FormatException: Unexpected character (at line 18, character 4) 
这个比较简单,类型转换错误
比如Map<String, String>接受了不是String的参数

5.NoSuchMethodError: The getter 'isNotEmpty' was called on null.  
isNotEmpty只能判断不为null的对象,比如空字符串,空数组,空字典,如果为null,就会报异常

6.RangeError (length): Invalid value: Valid value range is empty: 5 
数组越界

7.NoSuchMethodError: The getter 'item' was called on null. 
Get方法获取参数,对象为null,加判空处理

8.Failed assertion: boolean expression must not be null 
bool值不能为空,加个判断,兜底false

9.type 'ErrorEntity<xxxxxxxx>' is not a subtype of type 'BaseListEntity<yyyyyyyy>'
数据解析时模型不匹配

10.ArgumentError Invalid argument(s)
举例:比如一个数组给banner赋值,在点击的时候,组件还没来得及给点击的目标赋对应的index,造成了array[null]

11.FileSystemException: Cannot open file, path = '/var/mobile/Containers/Data/Application/273E0A0A-1E50-46B6-AF27-777591893A95/Documents/NIMSDK/1fe7b5329187d66a878fb0dd3623a2f3/Global/Resources/49ff1c3b38967d0cae814985acd933b5.jpg' (OS Error: No such file or directory, errno = 2) 
文件路径错误

12.Null check operator used on a null value
使用provider通过notify要刷新的某一个组件被销毁了,在notify前判断下是该类否挂载(mounted),还可能出现的场景是loding,dispose等,如果留意下一些pub,会发现mounted在里面使用的还是挺多的。https://codingfire.blog.csdn.net/article/details/109864780

13.PlatformException(Webview is null, null, null, null)
这个去检查Webview就对了,不过有时候未必是错的

14.Bad state: No element 
遍历的时候操作每一个element(字典)中的参数,element中这个参数有可能为null,要先做判空处理

15.Bad state: Stream has already been listened to. 
暂时认定为Stream重复监听了某个对象

16.MissingPluginException
MissingPluginException(No implementation found for method xxxxxxxxxx on channel xxxxxxxxxxx)
Channel中调用原生的方法未实现

17.NoSuchMethodError
NoSuchMethodError: The method ‘xxxxxxxxx’ was called on null.
Receiver: null
Tried calling: xxxxxxxxxx()
调用的方法返回null

18._TypeError 
type 'Future<dynamic>' is not a subtype of type 'Function'
定义的方法没有加Future声明

19.FormatException 
FormatException: Unexpected character (at line 18, character 4)
			"originalPath": "announcement/announce/index.html", 
   ^
转json的时候发生了异常,检查数据模型和数据结构

20.PlatformException 
PlatformException(Error, Error while launching https://xxxxxxxxxx)
Url不合法

有遇到新的会继续补充,欢迎大家补充。