Flutter (Dio) 运算符 '>='无法在“int?”上调用因为它可能为空
我将 flutter 更新到 2.10,我的 Dio 突然开始从下面的代码中给出这个错误:
static Future getRequest(String url, queryParam) async { 列表数据=[];
SharedPreferences crypt = await SharedPreferences.getInstance();
String? auth = crypt.getString("access");
try {
var res = await dio.get(url,
queryParameters: queryParam,
options: Options(headers: {
"accept": "application/json",
// "Authorization": "Bearer $auth"
}));
if (res.statusCode >= 200 && res.statusCode <= 250) {
print("RESPONSE");
print(res.data);
print(res.data);
data.add({"status": "success", "message": res.data});
return data;
} else {
data.add({"status": "failed", "message": res.data});
return data;
}
}on DioError catch (e) {
if (e.response.statusCode >= 400 && e.response.statusCode <= 450) {
data.add({
"status": "failed",
"message": e.response.data["error"]["message"]
});
return data;
}
}
}
还有
我收到以下错误:
Error: Operator '>=' cannot be called on 'int?' because it is potentially null.
if (res.statusCode >= 200 && res.statusCode <= 250) {
:
Error: Operator '>=' cannot be called on 'int?' because it is potentially null.
if (e.response.statusCode >= 400 && e.response.statusCode <= 450) {
关于如何解决这个问题有什么想法吗?
I updated my flutter to 2.10 and my Dio suddenly starts giving me this error from the code below:
static Future getRequest(String url, queryParam) async {
List data = [];
SharedPreferences crypt = await SharedPreferences.getInstance();
String? auth = crypt.getString("access");
try {
var res = await dio.get(url,
queryParameters: queryParam,
options: Options(headers: {
"accept": "application/json",
// "Authorization": "Bearer $auth"
}));
if (res.statusCode >= 200 && res.statusCode <= 250) {
print("RESPONSE");
print(res.data);
print(res.data);
data.add({"status": "success", "message": res.data});
return data;
} else {
data.add({"status": "failed", "message": res.data});
return data;
}
}on DioError catch (e) {
if (e.response.statusCode >= 400 && e.response.statusCode <= 450) {
data.add({
"status": "failed",
"message": e.response.data["error"]["message"]
});
return data;
}
}
}
}
I get the following errors:
Error: Operator '>=' cannot be called on 'int?' because it is potentially null.
if (res.statusCode >= 200 && res.statusCode <= 250) {
and also:
Error: Operator '>=' cannot be called on 'int?' because it is potentially null.
if (e.response.statusCode >= 400 && e.response.statusCode <= 450) {
Any Idea on how to solve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是健全的空安全https://dart.dev/null-safety的一部分,这意味着您的状态代码可以为空,并且空值不能与运算符进行比较(您可以只检查它们是否不为空)
您有多种选择来克服这种
非空强制:
在这种情况下,您事先检查状态代码是否不为空,并
根据您的结构强制使用非空值,您可能需要在变量后添加
!
以使编译器了解您知道变量不为空另一种选择是使用空安全运算符
??
这告诉编译器在空值的情况下该怎么做这表现为“如果 res.statusCode 不为空,则分配值,否则分配 -1" 那么您可以自由使用新的
statusCode
就像您在 null 安全之前所做的那样,从现在开始,statusCode
值不能再为 nullThis is part of sound null safety https://dart.dev/null-safety, it means that your status code can be null and null values can't be compared with operators (you can just check if they are not null)
you have several options to overcome this
force not null:
In this case, you check if your status code is not null beforehand and force the not null value
depending on your structure you may need to add a
!
after your variable in order to make the compiler understand that you know the variable is not nullAnother option is to use the null safe operator
??
this tells the compiler what to do in case of a null valueThis behave as "if res.statusCode is not null, assign the value, otherwise assign -1" then you are free to use the new
statusCode
as you did before null safety, since now, thestatusCode
value can't be null anymore这是一个 null 安全错误,将:更改
为:
这将为 res 添加一个默认值,以防它为 null 并且错误将消失。
It' a null safety error, change:
to:
This will adding a default value to res in case it will be null and error will disapear.