Benjamin Jesuiter Benjamin Jesuiter - 1 month ago 12
Dart Question

Dartlang 'await' not waiting?

Now I'm really confused with async/await.

My problem (I made some comments into the code) :

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;
}


As stated in the source code, I expect to see (in this order):


  • getLinkStructure called

  • No Structure saved in database - create default structure

  • dbKey: structure (will be returned if saving was successful



But instead the last dbkey log i get


  • data: INSTANCE OF TabEntry



and then


  • Error with storage initialization: Invalid argument: null
    in outer function



Why does
await _db.save(serializeToJson(structure), "structure")
not wait?


EDIT:

So this is the _db.save function which gets used:

@override
Future save(String obj, String key) {
return _runInTxn((store) => store.put(obj, key));
}


And this is _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;


}

Answer

For some reason, the Problem with the await statement disappeared. But as I had similar occurrences before, I will watch this in future and see, whether there is some await bug.

You can find further discussion about this problem / about solving the null issue (*) at

https://github.com/dart-lang/sdk/issues/27469

(*) the null issue: Error with storage initialization: Invalid argument: null