首页 > 解决方案 > 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。

标签: androidflutterdart

解决方案


我不是 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;
      }
 }

推荐阅读