当前位置:网站首页>Flutter | firstwhere error reporting
Flutter | firstwhere error reporting
2022-07-24 00:22:00 【Stars without night】
Error code :
final array = [1, 2, 3];
final a = array.firstWhere((element) => element == 4);
print(a);
Console display :
Bad state: No element
Are you kidding me? , Without that element, you will directly report an error , It's too fragile .
Never ignore this problem , An error message means that the following code will not be executed , There must be bug.
What will be wrong ?
The official document says :
If no element satisfies test, the result of invoking the orElse function is returned. If orElse is omitted, it defaults to throwing a StateError.
If no matching element is found , Will execute orElse function , without orElse function , Will throw out StateError.
Solution 1:
Use try catch Capture exception :
try {
final array = [1, 2, 3];
int? a = array.firstWhere((element) => element == 4);
print(a);
} catch (error) {
// Without that element
// Do the processing
}
Solution 2:
Pass in orElse Callback , Return directly when there is no specified element null:
final List<int?> array = [1, 2, 3];
int? a = array.firstWhere(
(element) => element == 4,
orElse: () => null,
);
print(a);
Console printing null.
边栏推荐
- L2TP的LAC自动拨号实验
- sed 深入理解与使用
- Intel Intel realsense realistic depth camera self calibration operation steps explanation D400 series is applicable
- IIS deployment.Netcore
- Multi knapsack explanation of dynamic programming knapsack problem
- Redis cluster construction (cluster cluster mode, fragment cluster)
- JMeter中的自动转义处理
- Gbase 8C session information function (4)
- It basic English
- 2022年7月23日——mapper文件说明
猜你喜欢

数仓数据标准详解-2022

Difference between data index and label system of data warehouse

vulnhub wpwn: 1

Inode, soft link, hard link

【Android Kotlin】Property、Getter 和 Setter

如何提升数据质量

Docker builds sonarqube, mysql5.7 environment

Xmind用例导入到TAPD的方案(附代码)

The implementation in Oracle replaces the specified content of the specified column with the desired content

Pipeline pipeline project is built by declarative and jenkinsfile under Jenkins
随机推荐
高数_第1章空间解析几何与向量代数__两点距
GBase 8c 访问权限查询函数(一)
Pytest interface automated testing framework | common running parameters of pytest
Gbase 8C access authority query function (I)
GBase 8c 访问权限访问函数(四)
Gbase 8C session information function (6)
数据模型设计方法概述
Gbase 8C string operator
[Android kotlin] property, getter and setter
kubernetes error
Inftnews | protect the "Snow Mountain Spirit", and the 42verse "digital ecological protection" public welfare project is about to start
A good habit to develop when writing SQL
July 23, 2022 - mapper file description
[speech synthesis] tensorflowtts Chinese text to speech
How to open a low commission account? Is it safe?
YOLOv1
GBase 8c系统表信息函数(二)
Try new methods
vulnhub wpwn: 1
sed 深入理解与使用