Can anyone help me how async
and await
work? The program complains that db
is null
when calling rawQuery(...)
. What I understood is the next statements after await
won't be executed when function with await
is not done yet, but the statements after an async
functions won't wait for that function to finish its job. For example:
var db;
Widget build(BuildContext context) {
...
List<Map<String, Object>> resultSet;
asyncFunction();
db.rawQuery(...).then((value) => resultSet = value);
return Scaffold(
appBar: AppBar(...),
child: Text()
);
}
void asyncFunction() async {
String dbPath = await getDatabasesPath();
String path = join(dbPath, 'sample.db');
db = await openDatabase(path);
}
db
is null
in db.rawQuery(...)
. (I hope you get my point). I don't know if it is possible to put return Scaffold()
inside then(...)
or is it possible for Widget build() {...}
to wait till all data are ready.