扑:未来不工作,它让我错误?(flutter:future is not working ,it s

2019-09-30 01:33发布

在我下面的代码的未来是不工作...编辑代码如下################################### ################################################## ################################################## ################################################## ################################################## ################################################## ################################################## ############

     void compressImage() async {
    print('startin');
    final tempDir = await getTemporaryDirectory();
    final path = tempDir.path;
    int rand = new Math.Random().nextInt(99999999);

    Im.Image image = Im.decodeImage(file.readAsBytesSync());
    Im.copyResize(image, 500);

//    image.format = Im.Image.RGBA;
//    Im.Image newim = Im.remapColors(image, alpha: Im.LUMINANCE);

    var newim2 = new File('$path/img_$rand.jpg')
      ..writeAsBytesSync(Im.encodeJpg(image, quality: 85));

    setState(() {
      file = newim2;
    });
    print('done');
  }

  void clearImage() {
    setState(() {
      file = null;
    });
  }

  void postImage() {
    setState(() {
      uploading = true;
    });
    compressImage();
    Future<String> upload = uploadImage(file).then((String data) {
      Firestore.instance.collection("Advertice").document().setData({
        "Content": discription,
        "title": title.toUpperCase(),
        "url": data,
      });
    }).then((_) {
      setState(() {
        file = null;
        uploading = false;
      });
    });
  }
}


Future<String> uploadImage(var imageFile) async {
  var uuid = new Uuid().v1();
  StorageReference ref = FirebaseStorage.instance.ref().child("post_$uuid.jpg");
  StorageUploadTask uploadTask = ref.put(imageFile);
  Uri URL = (await uploadTask.future).downloadUrl;
  return URL.toString();
}

Answer 1:

试试这个:您将获得文件下载网址:

String downloadUrl;

Future<String> uploadImage(var imageFile) async {
  var uuid = new Uuid().v1();
  StorageReference ref = FirebaseStorage.instance.ref().child("post_$uuid.jpg");
  await ref.put(imageFile).onComplete.then((val) {
                val.ref.getDownloadURL().then((val) {
                  print(val);
                  downloadUrl = val; //Val here is Already String
                });
              });

  return downloadUrl;
}


Answer 2:

你可以这样做,并从该方法的返回值的下载网址。

Future<UploadTaskSnapshot> uploadImage(var imageFile) {
  var uuid = new Uuid().v1();
  StorageReference ref = FirebaseStorage.instance.ref().child("post_$uuid.jpg");
  StorageUploadTask uploadTask = ref.put(imageFile);
  // Uri downloadUrl = (await uploadTask.future).downloadUrl;
  return uploadTask.future;
}

你可以下载网址,如类中的任何地方这样:

String downloadUrl = uploadImage(imageFile).downloadUrl.toString();


Answer 3:

这应该工作:

Future<String> uploadImage(var imageFile) async {
  var uuid = new Uuid().v1();
  StorageReference ref = FirebaseStorage.instance.ref().child("post_$uuid.jpg");
  StorageTaskSnapshot snapshot = await ref.putFile(image).onComplete;
  return await snapshot.ref.getDownloadURL();
}


文章来源: flutter:future is not working ,it show me error?