2015-05-01 171 views
1

我有一個未來,這是一個SQL查詢的結果,在它我循環每個返回的行以將其添加到列表與地圖編碼爲JSON格式後來。 在此循環中,我將執行另一個查詢,具體取決於外部查詢每行的結果,並將這些行再次添加到地圖中。等待未來在循環

Future<Results> mysqlAllTags = mysqlCon.query(query).then((results){ 
    // Make new list as a part of the JsonObject 
    json.tags = new List(); 
    // Loop throught the row of the result from Mysql data 
    return results.forEach((row){ 
    // Create Map to put the word, value, rating and id into the JsonObject 
    Map data = new Map(); 

    // Put the Mysql data into the Map 
    data["word"] = row.word.toString(); 
    data["value"] = row.value.toString(); 
    data["rating"] = row.rating.toString(); 
    data["id_tag"] = row.id_tag.toString(); 
    data["replacing"] = null; 

    // Add the Map to the userPages list 
    json.tags.add(data); 
    }).then((e){ 
    for(var tag in json.tags){ 
     //Map dataReplacing = getReplacing(userId, row.id_tag.toString()); 
     String replacingTags = getReplacingSQL(tag['id_tag'].toString()); 
     mysqlCon.query(replacingTags).then((result){ 
     result.forEach((row1){ 
      Map map = new Map(); 

      map["word"] = row1.word.toString(); 
      map["value"] = row1.value.toString(); 
      map["id_tag"] = row1.id_replacing_tag.toString(); 
      tag["replacing"] = map; 
     }).then((e){ 
      print("then inner for called"); 
      return null; 
     }); 
     print("then inner for called"); 
     return null; 
     }); 
    } 

    print("outer for returned"); 
    // Send the data in UTF8 to the client 
    result = Helpers.formatJsonAndEncodeUtf8('OK', session:_session, data: [json]); 

    return null; 
    }).catchError((error){ 
    result = Helpers.formatJsonAndEncodeUtf8('ERROR 856284555 (Could not load tags)', session:_session); 
    }); 
}).catchError((error){ 
    result = Helpers.formatJsonAndEncodeUtf8('ERROR 2346644555 (Could not load tags)', session:_session); 
}); 

return Future.wait([mysqlAllTags]).then((e){ 
    print("future returned"); 
    return result; 
}); 

結果看起來像這樣:

外爲返回

=== TO CLIENT ===

{ 「狀態」:[{ 「消息」:」行 「 」csrfToken「: 」99「}], 」數據「:[{ 」標籤「:[{ 」詞「: 」甜瓜「, 」值「: 」11.0「, 」等級「: 」1「,」 id_tag「:」37「,」replacement「:null},........}]}}

=================

未來返回

然後內名爲

然後內名爲

然後內名爲

然後內名爲

然後內名爲

然後稱爲

然後內名爲

然後內稱爲

我怎麼能等到所有期貨在我的for循環完成?

+0

你有沒有考慮過使用async/await。這使得關於代碼的理由變得更容易。 –

+1

你應該用'dartfmt'來格式化你的代碼,這樣會更容易閱讀。 – Pacane

回答

0

我不確定這是否能解決所有問題,在這樣的代碼中看到什麼並不容易返回未來。 (我添加了評論,我添加了一些東西)

Future<Results> mysqlAllTags = mysqlCon.query(query).then((results){ 
    // Make new list as a part of the JsonObject 
    json.tags = new List(); 
    // Loop throught the row of the result from Mysql data 
    return results.forEach((row){ 
    // Create Map to put the word, value, rating and id into the JsonObject 
    Map data = new Map(); 

    // Put the Mysql data into the Map 
    data["word"] = row.word.toString(); 
    data["value"] = row.value.toString(); 
    data["rating"] = row.rating.toString(); 
    data["id_tag"] = row.id_tag.toString(); 
    data["replacing"] = null; 

    // Add the Map to the userPages list 
    json.tags.add(data); 
    }).then((e){ 
    var futures = []; // added 
    for(var tag in json.tags){ 
     print("row "); 
     print(json.tags); 
     //Map dataReplacing = getReplacing(userId, row.id_tag.toString()); 
     String replacingTags = getReplacingSQL(tag['id_tag'].toString()); 

     // added `futures.add(...)` 
     futures.add(mysqlCon.query(replacingTags).then((result) { 
     result.forEach((row1){ 
      Map map = new Map(); 

      map["word"] = row1.word.toString(); 
      map["value"] = row1.value.toString(); 
      map["id_tag"] = row1.id_replacing_tag.toString(); 
      tag["replacing"] = map; 
     }).then((e){ 
      print("then inner for called"); 
      return null; 
     }); 
     print("then inner for called"); 
     return null; 
     })); 
    } 

    print("outer for returned"); 
    // Send the data in UTF8 to the client 
    result = Helpers.formatJsonAndEncodeUtf8('OK', session:_session, data: [json]); 

    return Future.wait(futures); // added 
    }).catchError((error){ 
    result = Helpers.formatJsonAndEncodeUtf8('ERROR 856284555 (Could not load tags)', session:_session); 
    }); 
}).catchError((error){ 
    result = Helpers.formatJsonAndEncodeUtf8('ERROR 2346644555 (Could not load tags)', session:_session); 
}); 

return Future.wait([mysqlAllTags]).then((e){ 
    print("future returned"); 
    return result; 
}); 
+0

這個解決方案不起作用,我想因爲當Future.wait(futures)被調用時,數組可以是空的或者沒有完全填充,所以它不會等待,所以它會返回 – fsulser

1

我通過在Future.wait(futures).then()函數中添加結果聲明來解決它。感謝GünterZöchbauer提供的意見。

Future<Results> mysqlAllTags = mysqlCon.query(query).then((results){ 
    // Make new list as a part of the JsonObject 
    json.tags = new List(); 
    // Loop throught the row of the result from Mysql data 
    return results.forEach((row){ 
    // Create Map to put the word, value, rating and id into the JsonObject 
    Map data = new Map(); 

    // Put the Mysql data into the Map 
    data["word"] = row.word.toString(); 
    data["value"] = row.value.toString(); 
    data["rating"] = row.rating.toString(); 
    data["id_tag"] = row.id_tag.toString(); 
    data["replacing"] = null; 

    // Add the Map to the userPages list 
    json.tags.add(data); 
}).then((e){ 
    var futures = []; // added 
    for(var tag in json.tags){ 
    print("row "); 
    print(json.tags); 
    //Map dataReplacing = getReplacing(userId, row.id_tag.toString()); 
    String replacingTags = getReplacingSQL(tag['id_tag'].toString()); 

    // added `futures.add(...)` 
    futures.add(mysqlCon.query(replacingTags).then((result) { 
     result.forEach((row1){ 
     Map map = new Map(); 

     map["word"] = row1.word.toString(); 
     map["value"] = row1.value.toString(); 
     map["id_tag"] = row1.id_replacing_tag.toString(); 
     tag["replacing"] = map; 
     }).then((e){ 
     print("then inner for called"); 
     return null; 
     }); 
     print("then inner for called"); 
     return null; 
    })); 
    } 

    print("outer for returned"); 
    // Send the data in UTF8 to the client 

    return Future.wait(futures).then((e){ 
     result = Helpers.formatJsonAndEncodeUtf8('OK', session:_session, data: [json]); 
    }); // added 
    }).catchError((error){ 
    result = Helpers.formatJsonAndEncodeUtf8('ERROR 856284555 (Could not load tags)', session:_session); 
    }); 
}).catchError((error){ 
    result = Helpers.formatJsonAndEncodeUtf8('ERROR 2346644555 (Could not load tags)', session:_session); 
}); 

return Future.wait([mysqlAllTags]).then((e){ 
    print("future returned"); 
    return result; 
});