I'm trying to create Sqflite database in android, and insert upon creation some records in it. I don't want to have an asset_db, because I have to insert only 5-6 records in one table.
I tried to do something like this
initDatabase() async {
String path = join(await getDatabasesPath(), DATABASE_NAME);
print('Init Database');
return await openDatabase(
path,
version: DATABASE_VERSION,
onOpen: (db) async {
print("Opened Database $DATABASE_NAME");
},
onCreate: _onCreate,
);
}
_onCreate(Database db, int version) async {
print("Creating new database with version $version");
await db.execute("CREATE TABLE IF NOT EXISTS categories ("
"id INTEGER PRIMARY KEY,"
"name VARCHAR(50) NOT NULL,"
")");
await _createDefaultCategories();
}
In _createDefaultCategories I do a couple of inserts using db.
The problem I have is that when I use the database and the init is called I got into an infinite loop printing forever:
I/flutter (10003): Creating new database with version 1
I/flutter (10003): Init Database
I/flutter (10003): Creating new database with version 1
I/flutter (10003): Init Database
I/flutter (10003): Creating new database with version 1
I/flutter (10003): Init Database
I/flutter (10003): Creating new database with version 1
What am I doing wrong ?
LE: The _createDefaultCategories is something like that:
_createDefaultCategories() async {
var database = await DBHelper.instance.database;
await database.insert('Category', Category(id: 1).toMap());
}
And the helper where I'm initializing the database is:
class DBHelper {
DBHelper._();
static final DBHelper instance = DBHelper._();
static Database _database;
Future<Database> get database async {
if(_database != null) {
return _database;
}
_database = await initDatabase();
return _database;
}
initDatabase() {
...above code...
}
}
_createDefaultCategories? It looks like you're callinginitDatabasefrom_createDefaultCategories?