Flutter Sqflite - 用户注销时删除数据库



我正在为我的应用程序使用 Sqflite 数据库来存储用户数据。问题是当用户注销时,我想删除数据库以避免在用户使用其他帐户登录时发生冲突,例如我不想在新登录时显示早期用户数据,对吗?

因此,当我删除数据库时,它似乎工作正常,但是当用户再次登录时,我收到以下错误:

[VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The method 'query' was called on null.

接收器:空

我想问题是_databaseVersion总是 1,所以我想知道我该如何解决这个问题?

.CLASS

class DatabaseHelper {
static final _databaseName = "MyDatabase.db";
static final _databaseVersion = 1;
static final table = 'my_table';
static final columnId = '_id';
static final columnCity = 'city';
static final columnAge = 'age';
static final columnAds = 'ads';
// make this a singleton class
DatabaseHelper._privateConstructor();
static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
// only have a single app-wide reference to the database
static Database _database;
Future<Database> get database async {
if (_database != null) return _database;
// lazily instantiate the db the first time it is accessed
_database = await _initDatabase();
return _database;
}
// this opens the database (and creates it if it doesn't exist)
_initDatabase() async {
print("init database");
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, _databaseName);
await openDatabase(path, version: _databaseVersion, onCreate: _onCreate);
}
// SQL code to create the database table
Future _onCreate(Database db, int version) async {
await db.execute('''
CREATE TABLE $table (
$columnId INTEGER PRIMARY KEY,
$columnCity TEXT,
$columnAge TEXT,
$columnAds INT
)
''');
///NOTE: columnAds 0 = true, 1 = false
}
// Helper methods
// Inserts a row in the database where each key in the Map is a column name
// and the value is the column value. The return value is the id of the
// inserted row.
Future<int> insert(Map<String, dynamic> row) async {
Database db = await instance.database;
return await db.insert(table, row);
}
// All of the rows are returned as a list of maps, where each map is
// a key-value list of columns.
Future<List<Map<String, dynamic>>> queryAllRows() async {
Database db = await instance.database;
return await db.query(table);
}
// All of the methods (insert, query, update, delete) can also be done using
// raw SQL commands. This method uses a raw query to give the row count.
Future<int> queryRowCount() async {
Database db = await instance.database;
return Sqflite.firstIntValue(
await db.rawQuery('SELECT COUNT(*) FROM $table'));
}
// We are assuming here that the id column in the map is set. The other
// column values will be used to update the row.
Future<int> update(Map<String, dynamic> row) async {
Database db = await instance.database;
int id = row[columnId];
return await db.update(table, row, where: '$columnId = ?', whereArgs: [id]);
}
// Deletes the row specified by the id. The number of affected rows is
// returned. This should be 1 as long as the row exists.
Future<int> delete(int id) async {
Database db = await instance.database;
return await db.delete(table, where: '$columnId = ?', whereArgs: [id]);
}
Future<bool> deleteDb() async {
bool databaseDeleted = false;
try {
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, _databaseName);
await deleteDatabase(path).whenComplete(() {
databaseDeleted = true;
}).catchError((onError) {
databaseDeleted = false;
});
} on DatabaseException catch (error) {
print(error);
} catch (error) {
print(error);
}
return databaseDeleted;
}
Future closeDb() async {
var dbClient = await instance.database;
dbClient.close();
}

您的帮助程序类始终使用_databaseVersion = 1

我认为,这就是为什么版本总是1的原因。

通常您不想删除数据库本身。如果是这样,则需要在应用程序启动时再次创建数据库,否则没有查询来运行与数据库相关的函数,因此会发生错误。 我的建议是删除数据库中的列(表(。

// Delete LoginResponse records
Future<int> deleteLoginResponse(int id) async {
final db = await dbProvider.database;
var result = await db.delete(loginTABLE, where: 'id = ?', whereArgs: [id]);
return result;
}

最新更新