在尝试将byteArray上传到firebase storage中的存储桶时,文件会上传到存储中,但我无法从文件中取回downloadUrl。我是这样获取bucket的引用的:
Future<Reference> get storageRef async {
final _bucketUrl = await bucketUrl;
return FirebaseStorage.instanceFor(bucket: _bucketUrl).ref();
}并像这样上传图片:
Future<String> uploadImageByteArray({
@required Uint8List byteArray,
String fileName,
}) async {
final name = fileName ?? DateTime.now().toIso8601String();
final _ref = await storageRef;
final _refUrl = _ref.child("images/$name.png");
print(_refUrl.fullPath);
final uploadTask = _refUrl.putData(byteArray);
final snapshot = await uploadTask;
return snapshot.ref.getDownloadURL();
}从上面的代码中,我得到了这个错误:
Unhandled Exception: type 'NoSuchMethodError' is not a subtype of type 'Exception'.如果我只获得FirebaseStorage的引用,而不是像下面这样的bucket,那么它就可以工作:
Future<Reference> get storageRef{
return FirebaseStorage.instance.ref();
}如果不使用存储桶引用,我就无法实现,因为根据租户的不同,可能会有不同的存储桶urls。我做错了什么?
编辑=>最近的发展:我发现如果我从_refUrl本身获取下载from,它就可以工作。即:
String downloadUrl = _refUrl.getDownloadUrl();它可以工作,但我不禁想知道它的实现是否正确。
发布于 2021-01-24 19:19:56
您的编辑非常有意义,因为您已经使用_refUrl引用了上传的文件,获取其长期下载URL的工作方式与预期的一样(这就是我在btw之前所做的)。我无法使用FirebaseStorage访问项目来测试这一点,但您可以尝试打印snapshot.ref.fullPath,并将其与_refUrl的fullPath进行比较。
发布于 2021-01-24 20:59:59
尝尝这个
Future<String> uploadImageByteArray({
@required Uint8List byteArray,
String fileName,
}) async {
final name = fileName ?? DateTime.now().toIso8601String();
final _ref = await storageRef;
final _refUrl = _ref.child("images/$name.png");
print(_refUrl.fullPath);
final uploadTask = _refUrl.putData(byteArray);
final snapshot = (await uploadTask);
String url = await snapshot.ref.getDownloadURL(); // await
return url;
}https://stackoverflow.com/questions/65867036
复制相似问题