扑:未来不起作用,它告诉我错误吗? [英] flutter:future is not working ,it show me error?

查看:84
本文介绍了扑:未来不起作用,它告诉我错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我下面的代码中,将来无法正常工作...已编辑的代码在#################################以下############################################### ############################################### ############################################### ################################################ ############################################### ################################################ ###############

In my below code future is not working ...edited code is below ###########################################################################################################################################################################################################################################################################################################################################################

     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();
}


推荐答案

尝试一下:您将获得文件下载URL:

Try this: You will get the File Download URL:

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;
}

这篇关于扑:未来不起作用,它告诉我错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆