gpt4 book ai didi

Flutter 主体可能正常完成,导致返回 'null',但返回类型是潜在的不可空类型?

转载 作者:行者123 更新时间:2023-12-03 08:13:23 25 4
gpt4 key购买 nike

我正在使用 flutter 从 API 返回数据,但我遇到问题告诉我

The body might complete normally, causing 'null' to be returned, but the
return type is a potentially non-nullable type.
Try adding either a return or a throw statement at the end.

这是我的方法:

Future<void> getDoctorsFromApi() async {
List<int> ids = await findAllDoctor().then((list) {
return list.map((e) => e.syncedId).toList();
});
doctors = await DoctorApi.getDoctors(ids).then((response) { // this is the line where error occurs
if (response.statusCode == 200) {
Iterable list = json.decode(response.body);
return list.map((model) => Doctor.fromJson(model)).toList();
} else {
_showMyDialog();
}
});
setState(() {
insertDoctors(database!);
});
}

最佳答案

如果 response.statusCode 不是 200,doctors 的值是多少?处理那个通过创建可为 null 的局部变量:

final List<Doctor>? result = await DoctorApi.getDoctors(ids).then((response) {
if (response.statusCode == 200) {
Iterable list = json.decode(response.body);
return list.map((model) => Doctor.fromJson(model)).toList();
}
return null;
});

if (result == null) {
_showMyDialog();

} else {
doctors = result;
setState(() => insertDoctors(database!));
}

关于Flutter 主体可能正常完成,导致返回 'null',但返回类型是潜在的不可空类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70175478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com