我在Dart中有一个函数,我想返回一个值来传播一个失败(左)或一个对象(右)。这些数据依赖于函数中的不同值,例如Dart中的this,但目前它返回的是null,即使输入返回的是(右)值。
Future<Either<Failure, User>> getUser(String uid) async {
Either<Failure, DocumentSnapshot> document;
document = await _database.getDocumentById(uid);
// _database.getDocument function returns a Either<Failure, Document> object
// But when I fold the value to propagate the failure or return an User, I get null as
//return
document.fold((failure) => Left(failure), (document) => Right(User.fromJson(document.data)));
}User.fromJson函数已经过测试,并且工作正常,但我无法返回这两个对象
发布于 2020-07-04 02:57:22
如果函数没有返回语句,则Dart隐式返回null。
https://dart.dev/tools/diagnostic-messages#missing_return
因此,只需添加一个return语句就可以解决您的问题。
https://stackoverflow.com/questions/61940828
复制相似问题