You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I use floor package to auto generate sqflite database in my project. I tried to close the current database and used $FloorAppDatabase.databaseBuilder(dbPath).build() to switch to the new one but it kept the old data of the previous database.
Here is my code:
@Database(version:1, entities: [
Customer,
Product,
])
abstractclassAppDatabaseextendsFloorDatabase {
AppDatabase();
CustomerDaoget customerDao;
ProductDaoget productDao;
Future<void> deleteAllTables() async {
await database.execute('DELETE FROM Customer');
await database.execute('DELETE FROM Product');
}
}
classDatabaseService {
AppDatabase? _currentDatabase;
CustomerDao? _customerDao;
ProductDao? _productDao;
Future<AppDatabase> _initDatabase(String dbPath) async {
final database =await$FloorAppDatabase.databaseBuilder(dbPath).build();
return database;
}
Future<void> switchDatabase(int? userId) async {
// Close the current database if it existsawait_closeCurrentDatabase();
// Initialize the new database
_currentDatabase =await_initDatabase('${userId?.toString() ?? AppConstants.APP_DATABASE_NAME}.db');
_customerDao = _currentDatabase?.customerDao;
_productDao = _currentDatabase?.productDao;
}
CustomerDaoget customerDao => _customerDao!;
ProductDaoget productDao => _productDao!;
Future<void> _closeCurrentDatabase() async {
await _currentDatabase?.close();
_currentDatabase =null;
}
}
The text was updated successfully, but these errors were encountered:
I'm not familiar with floor so I cannot say whether it is correct or not. Have you reported this issue in floor (https://github.com/pinchbv/floor) first?
I use floor package to auto generate sqflite database in my project. I tried to close the current database and used $FloorAppDatabase.databaseBuilder(dbPath).build() to switch to the new one but it kept the old data of the previous database.
Here is my code:
The text was updated successfully, but these errors were encountered: