How to get full downloadUrl from UploadTaskSnapshot in Flutter?

前端 未结 4 488
感情败类
感情败类 2020-11-29 11:59

I correctly receive UploadTaskSnapshot, and the field downloadUrl contains an instance of Uri that parses download link of uploaded file.

H

相关标签:
4条回答
  • 2020-11-29 12:37

    I get downloadUrl from v1.0.3 by the following code.

    StorageReference storageReference = _storage.ref().child(path);
    StorageUploadTask uploadTask = storageReference.putFile(imageFile);
    
    StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
    
    String downloadUrl = await taskSnapshot.ref.getDownloadURL();
    
    0 讨论(0)
  • 2020-11-29 12:41

    old

    final uploadTask = imageStore.putFile(imageFile);
    final url = (await uploadTask.future).downloadUrl;
    

    update

    This answer https://stackoverflow.com/a/52690889/217408 is now the accurate one.

    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;
    

    or

    var url = Uri.parse(await ref.getDownloadURL() as String);
    
    0 讨论(0)
  • 2020-11-29 12:53

    From my this answer,

    The latest version of plugin doesn't let you use task.future() anymore and in the documentation they say to use lastSnapshot which didn't work for me.

    So, I used onComplete. Here is the working solution:

    StorageUploadTask uploadTask = storageRef.putFile(yourFile);
    StorageTaskSnapshot storageTaskSnapshot = await uploadTask.onComplete;
    String downloadUrl = await storageTaskSnapshot.ref.getDownloadURL();
    
    0 讨论(0)
  • 2020-11-29 12:59

    @DomingoMG it looks like with the latest release they want:

    String location = await ref.getDownloadURL();
    

    See https://pub.dartlang.org/packages/firebase_storage#-example-tab-

    0 讨论(0)
提交回复
热议问题