Download an image from Firebase to Flutter

2020-04-08 14:45发布

问题:

There are many examples of uploading a file to firebase and getting a downloadUrl but there's no example that I found to get the DownloadURL for an image and using it in a Flutter widget.

This is the one which is related to uploading a file

final StorageReference ref =
    FirebaseStorage.instance.ref().child("foo$rand.txt");
final StorageUploadTask uploadTask = ref.put(file);
final Uri downloadUrl = (await uploadTask.future).downloadUrl;
final http.Response downloadData = await http.get(downloadUrl);

Can anyone give an example of one where the file already exists in firebase and how to get a download link?

回答1:

You can just use the Storage API to download, like with upload

someMethod() async {
  var data = await FirebaseStorage.instance.ref().child("foo$rand.txt").getData();
  var text = new String.fromCharCodes(data);
  print(data);
}