我正确接收了
UploadTaskSnapshot
,并且字段downloadUrl包含一个Uri
的实例,它解析上传文件的下载链接。
如何获取字符串形式的存储和下载Url?
老
final uploadTask = imageStore.putFile(imageFile);
final url = (await uploadTask.future).downloadUrl;
更新
这个答案https://stackoverflow.com/a/52690889/217408现在是准确的。
final ref = FirebaseStorage.instance
.ref()
.child('path')
.child('to')
.child('the')
.child('image_filejpg');
ref.putFile(imageFile);
// or ref.putData(Uint8List.fromList(imageData));
var url = await ref.getDownloadURL() as String;
或
var url = Uri.parse(await ref.getDownloadURL() as String);
我通过以下代码从 v1.0.3 获得
downloadUrl
。
StorageReference storageReference = _storage.ref().child(path);
StorageUploadTask uploadTask = storageReference.putFile(imageFile);
StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
String downloadUrl = await taskSnapshot.ref.getDownloadURL();
@DomingoMG 看起来像是他们想要的最新版本:
String location = await ref.getDownloadURL();
见https://pub.dartlang.org/packages/firebase_storage#-example-tab-
更新:2020 年 11 月
onComplete
现在已从上传任务中删除。所以,使用:
var reference = FirebaseStorage.instance.ref().child("your_path");
await reference.putFile(fileToUpload);
var url = await reference.getDownloadURL();
使用 firebase_storage 的版本:^11.0.11。 它是这样工作的:
final FirebaseFirestore firestore = FirebaseFirestore.instance;
final FirebaseStorage storage = FirebaseStorage.instance;
DocumentReference get firestoreRef => firestore.doc('products/$id');
Reference get storageRef => storage.ref().child('products').child(id!);
final UploadTask task = storageRef.child(const Uuid().v1()).putFile(newImage as File);
final TaskSnapshot snapshot = await task.whenComplete(() {});
final String url = await snapshot.ref.getDownloadURL();
updateImages.add(url);
OBS:我正在使用 package uuid: ^3.0.7 为每次上传创建一个随机 ID