我正在尝试将图像上传到Firebase存储,但是当我调用该函数时,并未执行await来获取URL。我在这里想念什么?
看看另一个主题,我可能是问题是“ then”,但是我该如何设置代码以等待url?
Async/Await/then in Dart/Flutter
Future < String > uploadImage(File imageFile) async {
String _imageUrl;
StorageReference ref =
FirebaseStorage.instance.ref().child(firebaseUser.uid.toString());
await(ref.putFile(imageFile).onComplete.then((val) {
val.ref.getDownloadURL().then((val) {
_imageUrl = val;
print(val);
print("urlupload");
});
}));
print(_imageUrl);
print("urlnoupload");
return _imageUrl;
}
谢谢!
答案 0 :(得分:0)
您不需要异步时等待/等待,因为您正在then
函数中获取价值
Future<String> uploadImage(File imageFile) async {
String _imageUrl;
StorageReference ref = FirebaseStorage.instance.ref().child(firebaseUser.uid.toString());
return ref.putFile(imageFile).onComplete.then((val) {
return val.ref.getDownloadURL()
}).then((_imageUrl) {
return _imageUrl;
});
},
答案 1 :(得分:0)
我强烈建议您使用async / await,更易读且clean code。
Future <String> uploadImage(File imageFile) async {
String _imageUrl;
// Create the reference
StorageReference ref = FirebaseStorage.instance.ref().child(firebaseUser.uid.toString());
// Upload the file
StorageTaskSnapshot storageSnapshot = await ref.putFile(imageFile).onComplete;
// Get the DownloadUrl
_imageUrl = await ref.getDownloadURL();
print("File url:"+ _imageUrl);
return _imageUrl;
}