Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

dart - Flutter cannot get download url firebase-storage

While trying to upload byteArray to the bucket in firebase storage, the file uploads to the storage but I cannot get the downloadUrl back from the file. I am getting the reference of bucket like this:

  Future<Reference> get storageRef async {
    final _bucketUrl = await bucketUrl;
    return FirebaseStorage.instanceFor(bucket: _bucketUrl).ref();
  }

And Uploading image like this:

 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();
  }

From above code I am getting this error:

Unhandled Exception: type 'NoSuchMethodError' is not a subtype of type 'Exception'.

It works if I get reference for the FirebaseStorage only and not the bucket like this:

 Future<Reference> get storageRef{
    return FirebaseStorage.instance.ref();
  }

I cannot implement without using bucket reference because there can be different bucket urls depending on the tenants. What am I doing wrong?

Edit => Recent Developments: I found out that it works if I get the downloadurl from the _refUrl itself. i.e:

String downloadUrl = _refUrl.getDownloadUrl();

It works but I can't help but wonder if it is correct implementation.

question from:https://stackoverflow.com/questions/65867036/flutter-cannot-get-download-url-firebase-storage

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your edit makes perfect sense since you have the reference to the uploaded file with _refUrl, fetching its long-lived download URL works as expected (That's how I have done it before btw). I don't have access to a project with FirebaseStorage to test this, but You can try printing snapshot.ref.fullPath and compare it with the fullPath of _refUrl.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...