我的未来是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 ===
{&#34;状态&#34;:[{&#34;消息&#34;:&#34; OK&#34;&#34; csrfToken&#34;:&#34; 99&#34; }],&#34;数据&#34;:[{&#34;标记&#34;:[{&#34;字&#34;:&#34;甜瓜&#34;&#34;值&#34 ;:&#34; 11.0&#34;&#34;评价&#34;:&#34; 1&#34;&#34; id_tag&#34;:&#34; 37&#34;&# 34;替换&#34;:空},........}]}
=================
未来回归
然后内部称为
然后内部称为
然后内部称为
然后内部称为
然后内部称为
然后内部称为
然后内部称为
然后内部称为
我怎么能等到for循环中的所有期货都完成?
答案 0 :(得分: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;
});
答案 1 :(得分:0)
我不确定这是否解决了所有问题,在这样的代码中不容易看到究竟是什么导致了未来。 (我在添加内容的地方添加了评论)
healthkit.com/api/v1/user/GetWeight