【发布时间】:2020-01-05 08:00:41
【问题描述】:
为什么返回值总是空?
_repository.addUpdateWorkOrder(workOrder).listen((ref) {
image.id = ref.documentID;
_repository
.getImageDownloadUrl(ref.documentID, image.imagePath)
.then((val) {
print('The value of the input is: $val');
});
});
存储库
@override
Future<List<String>> getImageDownloadUrl(
String id, List<Asset> imagePath) async {
Future.wait(imagePath.map((Asset asset) async {
ByteData byteData = await asset.requestOriginal();
List<int> imageData = byteData.buffer.asUint8List();
var dateTime = new DateTime.now();
final StorageReference firebaseStorageRef = FirebaseStorage.instance
.ref()
.child('images/' + id + '/' + dateTime.toString() + ' .jpg');
return firebaseStorageRef.putData(imageData).onComplete.then(
(uploadTask) =>
uploadTask.ref.getDownloadURL().then((url) => url.toString()));
}));
}
输出
I/flutter(22901):输入的值为:null
【问题讨论】:
-
uploadTask也为空? -
@PeterHaddad 没有。当我调试时,我可以看到 url 上有值。
-
@PeterHaddad 在我调试时,它会先运行
print('The value of the input is: $val');,然后只运行getImageDownloadUrl方法... -
您不能从
then方法返回值,您应该改用await并将结果分配给新变量,然后再次使用它。我所说的只是getImageDownloadUrl函数中的代码。 -
@JohnJoe,请检查这个gist 是为你编码的,并告诉我它是否有效或发生了什么?
标签: firebase firebase-realtime-database flutter dart firebase-storage