现在我真的对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;
}
答案 0 :(得分:1)
由于某种原因,await语句的问题消失了。 但是,由于我之前有类似的事情发生,我将来会看到这一点,看看是否有一些等待错误。
您可以在
找到有关此问题/解决空问题(*)的进一步讨论https://github.com/dart-lang/sdk/issues/27469
(*)null问题:
Error with storage initialization: Invalid argument: null