【问题标题】:How to get snapshot ref.getDownloadURL()?如何获取快照 ref.getDownloadURL()?
【发布时间】:2019-09-22 23:53:50
【问题描述】:

我编写了一个程序将照片保存在 Firebase 存储中,然后获取一个 url 并将其写入 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();
          });

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

【问题讨论】:

    标签: android dart flutter firebase-storage


    【解决方案1】:

    等待完成上传过程;

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

    【讨论】:

    • 在我的项目中写入“未定义名称 UploadTask”。我需要导入一些东西或使用更新的版本?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 2017-05-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    • 2013-03-27
    • 1970-01-01
    相关资源
    最近更新 更多