在顫振中,我正在使用 xml 庫來構建 xml
final builder = XmlBuilder();
builder.processing('xml', 'version="1.0"');
builder.element("resources",
namespaces: {"http://schemas.example.com/settings": "settings"},
nest: await () async { // wait until this completes
await Future.forEach<String?>(keys, (element) async {
final transString = await sheet.values.rowByKey(element!);
builder.element("product", attributes: {"name": element},
nest: () {
builder.text(transString![allCols.indexOf("tay")]);
});
});
});
print(builder.buildDocument().toXmlString()); // this reaches before nest: function completes
現在我的問題是,在 builder.element(...)函式呼叫中,有一個nest構建 XML 內部節點的回呼
在里面nest我使用谷歌表格庫從谷歌表格中讀取資料并將其附加到 XML
由于sheet.values.rowByKey(..)是 Future 函式,我需要await獲取值并構建內部節點
我的問題是,
在該行之后builder.processing('xml', 'version="1.0"');,執行直接print(builder.buildDocument().toXmlString())在嵌套的回呼執行之前執行
也就是說,使用它的前 2 個引數執行,但它在執行回呼builder.element (..)之前跳到下一行 (print(..))nestasync
在此之后print,里面的塊nest被執行!!!
所以列印陳述句只列印帶有“<資源...>”標簽的XML,而不是內部標簽
我嘗試放置 await builder.element ,但它顯示錯誤,因為 builder.element 不回傳 Future only void
那么,如何讓 builder.element 等到它的nest塊完成執行然后繼續print
uj5u.com熱心網友回復:
有很多方法可以等待資料。我向您展示了我最常用的兩種方法。
未來建造者:
FutureBuilder( future:yourMethod(), builder:(context,snapshot){ if(snapshot.hasData){ //Code Here return Container(); } return CircularProgressIndicator(); })
2.使用方法:
Future<String> yourMethod() async{
String data=await getDataFromApi();
// Now you can use data variable inside code. its wait until data is assign
return data;
}
uj5u.com熱心網友回復:
嘗試通過這樣waiting輸入來完成您的功能:await
await builder.element("resources",
namespaces: {"http://schemas.example.com/settings": "settings"},
nest: await () async { // wait until this completes
await Future.forEach<String?>(keys, (element) async {
final transString = await sheet.values.rowByKey(element!);
builder.element("product", attributes: {"name": element},
nest: () {
builder.text(transString![allCols.indexOf("tay")]);
});
});
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/410862.html
標籤:
上一篇:傳遞給異步函式的引數范圍
下一篇:如何在反應中異步渲染功能組件?
