现在,我真的对async / await感到困惑。
我的问题(我对代码做了一些注释):
Future<List<TabEntry>> getLinkStructure() async {
_log.finest("getLinkStructure called");
String stringData = await _db.getByKey("structure");
if (stringData == null) {
_log.finest("No Structure saved in database - create default structure");
List<TabEntry> structure = [];
structure.add(new TabEntry("Home"));
//All things are working till here
//Problem: Create key in db - this apparently does not wait
String key = await _db.save(serializeToJson(structure), "structure");
//I EXPECT THE FOLLOWING LOG NEXT
_log.finest("dbKey: $key");
if (key == "structure")
return structure;
else
throw "Error while saving default Home tab in structure";
}
var data = deserialize(stringData);
//INSTEAD I GET THIS LOG
_log.finer("data: ${data}");
return data;
}
如源代码中所述,我希望看到(按此顺序):
但是我得到的最后一个dbkey日志
然后
外部函数中的
Error with storage initialization: Invalid argument: null
为什么
await _db.save(serializeToJson(structure), "structure")
不等待? 编辑:
因此,这是使用的_db.save函数:
@override
Future save(String obj, String key) {
return _runInTxn((store) => store.put(obj, key));
}
这是_runInTxn(...):
Future _runInTxn(Future requestCommand(idb.ObjectStore store),
[String txnMode = 'readwrite']) async {
var trans = _db.transaction(storeName, txnMode);
var store = trans.objectStore(storeName);
var result = await requestCommand(store);
await trans.completed;
return result;
}
最佳答案
由于某种原因,await语句的问题消失了。
但是由于我之前也有类似的情况发生,所以我以后会看这个,看看是否有一些等待中的错误。
您可以在以下位置找到有关此问题/解决空问题(*)的进一步讨论
https://github.com/dart-lang/sdk/issues/27469
(*)空问题:
Error with storage initialization: Invalid argument: null
关于asynchronous - Dartlang 'await'不等吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39820567/