如何获取快照ref.getDownloadURL()?

时间:2019-05-04 20:48:06

标签: android dart flutter firebase-storage

我制作了一个程序,将照片保存在Firebase存储中,然后获取一个网址并将其写入Firebase数据库。

File sampleImage;
  String changePhoto;

Future getImage() async {
    var tempImage = await ImagePicker.pickImage(source: ImageSource.gallery);
    setState(() {
      sampleImage = tempImage;
    });
  }

  Future getUrl(name) async
  {
    final ref = FirebaseStorage.instance.ref().child(name);
    changePhoto = await ref.getDownloadURL();
    print(changePhoto);
    return changePhoto;
  }

Widget submitButton() {
    final name = productName.text;
    return RaisedButton(
        textColor: Colors.white,
        color: Color(0xffd50000),
        child: Text("Add"),
        shape: RoundedRectangleBorder(
            borderRadius: new BorderRadius.circular(30.0)),
        onPressed: () {
          final StorageReference firebaseStorageRef =
          FirebaseStorage.instance.ref().child(name);
          final StorageUploadTask task =
          firebaseStorageRef.putFile(sampleImage);
          getUrl(name);
          _bloc.submit(changePhoto);
          print(5);
          Navigator.of(context).pop();
        });
  }

但是我对

有问题
  

changePhoto =等待ref.getDownloadURL();

因为ref.getDownloadURL()返回动态变量。我尝试使用

Future loadPhoto(name)
  {
    final StorageReference firebaseStorageRef =
    FirebaseStorage.instance.ref().child(name);
    final StorageUploadTask task =
    firebaseStorageRef.putFile(sampleImage);
  }

onPressed: () {
loadPhoto(name).then((snapshot){
            final ref = FirebaseStorage.instance.ref().child(name);
          changePhoto = snapshot.ref.getDownloadURL();
            print(changePhoto);
            _bloc.submit(changePhoto);
            print(5);
            Navigator.of(context).pop();
          });

但是它不起作用。我的错误是什么?如何获取快照? 非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

等待完成上传过程;

 var dowurl = await (await uploadTask.onComplete).ref.getDownloadURL();