I am very new to dart and follow a tutorial which was created in an earlier version of dart i think. However, the _database
variable throws the error
Non-nullable instance field '_database' must be initialized.
I am not sure but i think its because the type safety thing, therefore, the getter which will act as a "singleton mechanism" won't work and the _database must be initialized before. How can i create the database connection when its requested by the getter?
Thanks
class DatabaseProvider {
static final DatabaseProvider dbProvider = DatabaseProvider();
Database _database;
Future<Database> get database async {
if (_database != null) return _database;
_database = await createDatabase();
return _database;
}
createDatabase() async {
Directory documentDirectory = await getApplicationDocumentsDirectory();
String path = join(documentDirectory.path, "test.db");
Database database = await openDatabase(path);
return database;
}
}
PS: my IDE suggest to use the late keyword, which i read is a bad thing and should be avoided in that case, since we have also to check if its already inititialized - a feature dart does not provide(?)