android - Dart 函数返回 null
问题描述
我有一个函数调用请求在服务器中创建一个新学生
createStudents(String studentName) async {
await Webservice()
.postReq(new API().createNewStudent(context), studentName)
.then((value) {
if (value is Students) {
Students std = value;
helper.initializeDatabase();
helper.insertStudents(std);
return "Sucessfull";
} else {
return value;
}
});
}
在此示例中,我发送了学生的姓名,并在服务器中创建了新学生。
如果成功,它会返回一个学生对象。
如果不成功,则返回消息。
我在这个函数中调试和检查了值,但它总是返回 null。
成功时返回 Student 对象,但 createStudents 函数返回 null。错误信息相同
var ab = await createTags(data);
print(ab);
ab 始终为空。
我不知道为什么它总是返回 null。
解决方案
我不是 100% 确定,但我最好的猜测是你忘记了“return”声明
createStudents(String studentName) async {
//Here should be return
final value = await Webservice()
.postReq(new API().createNewStudent(context), studentName);
if (value is Students) {
Students std = value;
helper.initializeDatabase();
helper.insertStudents(std);
return "Sucessfull";
} else {
return value;
}
}
推荐阅读
- azure - 是否可以使用站点到站点 (VPN) 连接来连接两个 Azure VNet?
- db2 - 在okd上部署db2
- flutter - 使用反射循环所有可用的图标
- html - 如何使用 bootstrap 4 和 css 创建文件夹形状卡片作为单选按钮?
- reactjs - 如何编写代码以在 ReactJS 中使用 JSON 文件将数组数据发送到磁盘?
- c# - 如何在水晶报表中创建子报表,即产品详细信息、到期详细信息....点击主报表
- amazon-web-services - 我应该给予什么 aws 权限来访问其他 s3 存储桶
- node.js - nodejs从POST请求中获取原始正文
- reactjs - 我正在尝试在由 Formik 控制的字段上应用掩码,但它不起作用。该值正在覆盖前一个
- javascript - 带有 IP 地址的 Next.js api 路由