未来飞镖/颤振的返回值

时间:2019-09-02 09:12:14

标签: firebase firebase-realtime-database flutter dart firebase-storage

为什么返回值总是为空?

 _repository.addUpdateWorkOrder(workOrder).listen((ref) {
    image.id = ref.documentID;
    _repository
        .getImageDownloadUrl(ref.documentID, image.imagePath)
        .then((val) {
      print('The value of the input is: $val');
    });
  });

存储库

@override
  Future<List<String>> getImageDownloadUrl(
      String id, List<Asset> imagePath) async {
    Future.wait(imagePath.map((Asset asset) async {
      ByteData byteData = await asset.requestOriginal();
      List<int> imageData = byteData.buffer.asUint8List();

      var dateTime = new DateTime.now();
      final StorageReference firebaseStorageRef = FirebaseStorage.instance
          .ref()
          .child('images/' + id + '/' + dateTime.toString() + ' .jpg');

      return firebaseStorageRef.putData(imageData).onComplete.then(
          (uploadTask) =>
              uploadTask.ref.getDownloadURL().then((url) => url.toString()));
    }));
  }

输出

  

I / flutter(22901):输入的值为:null

1 个答案:

答案 0 :(得分:2)

在对问题发表评论时,我们终于达到了解决方案,就像我们在gist中编辑getImageDownloadUrl函数一样。

以及下面的代码

@override
Future<List<String>> getImageDownloadUrl(
    String id, List<Asset> imagePath) async {

  // Variable to hold the list of urls. 
  // Also I think you need only one String not List.
  List<String> list = List<String>();

  for (Asset asset in imagePath) { 

    ByteData byteData = await asset.requestOriginal();
    List<int> imageData = byteData.buffer.asUint8List();

    var dateTime = new DateTime.now();
    final StorageReference firebaseStorageRef = FirebaseStorage.instance
        .ref()
        .child('images/' + id + '/' + dateTime.toString() + ' .jpg');

    // Using await to hold the value that result.
    var uploadTask = await firebaseStorageRef.putData(imageData).onComplete;
    String url = await uploadTask.ref.getDownloadURL();

    // Add the url to list
    list.add(url);
  }

  return list;
}