扑:未来不起作用,它告诉我错误吗?

时间:2018-10-04 14:38:08

标签: firebase dart flutter firebase-storage

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

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

3 个答案:

答案 0 :(得分:3)

尝试:您将获得文件下载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;
}

答案 1 :(得分:0)

您可以执行此操作,并从此方法的返回值获取下载网址。

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

答案 2 :(得分:0)

这应该有效:

$con = mysqli_connect($host, $username, $pass); //variable for original connection
$db_con = mysqli_connect($host, $username, $pass, $dbname); //database connection


/*==== MYSQL Connection and Database Creation ====== */
function connectDb($con){
    //Create connection or die trying
    if (!$con){
        echo "<script>alert('Failed to connect to MySQL')</script>";
        exit(1);
    }
    else {
        echo "<p>Connected to MySQL successfully.\n</p>";
    }
    return;
}

function makeDb($con, $dbname){
    //creates the main database if it is not already created

    $db_select = mysqli_select_db($con, $dbname);
    $db_create = "CREATE DATABASE ".$dbname; //create database

    if(!$db_select) {

        /*database could not be selected or is not created so we'll go ahead
         * and create one and throw an error if we can't*/

        if (!mysqli_query($con, $db_create)){
            echo "<script>alert('Error creating database')</script>";
            exit(1);
        }
        else {
            echo "</p>Database created successfully. \n </p>";
        }

      }
    else{
        echo "<p>Database selected!</p>";
    }
    return;
}

/*===== CREATE THE TABLES ============*/

function createTableSQL($tabName, $columns){

    /*CREATES THE SQL Statement for Table Creation*/

    $sql = 'CREATE TABLE ' .$tabName.' (';

    for ($i = 0; $i < count($columns); $i++){
        $sql = $sql .= $columns[$i];

        if($i < count($columns)-1){
            $sql = $sql .= ", ";
        }

        else {
            $sql = $sql .=")";
        }
    }
    return $sql;
}
function createTable($db_con, $tabName, $sql){

    /*Creates the a table. The table name and sql statement variables are set 
    outside the function.*/
    $select_sql = "SELECT 1 from ".$tabName." Limit 1";

    if (!mysqli_query($db_con, $select_sql)){
        //table does not exit. Attempt to create table
        if (!mysqli_query($db_con, $sql)) {
            echo "<script>alert('Error creating the ".$tabName." table.')</script>";
            exit(1);
        }
        else{
            echo "<p>".$tabName." table created successfully!\n</p>";
        }
    }
    else{
        echo "<p>".$tabName." table exists!\n</p>";
    }
    return;

}

function playerTable($db_con){


    //Check if the Players Table exists and create it if it does not.

    $tabName = "Players";

    $columns = array(
    "id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY",
    "playername VARCHAR (30) NOT NULL",
    "charname VARCHAR (30)",
    "class VARCHAR (30)",
    "race VARCHAR (30)",
    "level INT(2)");

     $sql = createTableSQL($tabName, $columns);
     createTable($db_con, $tabName, $sql);

     return;
}

connectDb($con); //Make the database connection
makeDb($con, $dbname); //Create the database
playerTable($db_con); // Create the table