mirror of
https://gitlab.com/comunic/comunicmobile
synced 2025-06-19 08:15:16 +00:00
Start to fix null safety migration errors
This commit is contained in:
@ -9,11 +9,11 @@ import 'package:sqflite/sqflite.dart';
|
||||
/// @author Pierre HUBERT
|
||||
|
||||
abstract class DatabaseHelper {
|
||||
static Database _db;
|
||||
static Database? _db;
|
||||
|
||||
/// Open the database
|
||||
static Future<void> open() async {
|
||||
if (_db != null && _db.isOpen) return;
|
||||
if (_db != null && _db!.isOpen) return;
|
||||
|
||||
var databasePath = await getDatabasesPath();
|
||||
_db = await openDatabase(
|
||||
@ -24,7 +24,7 @@ abstract class DatabaseHelper {
|
||||
}
|
||||
|
||||
/// Get a database instance
|
||||
static Future<Database> get() async {
|
||||
static Future<Database?> get() async {
|
||||
await open();
|
||||
return _db;
|
||||
}
|
||||
|
@ -18,12 +18,12 @@ abstract class ModelDatabaseHelper<T extends CacheModel> {
|
||||
|
||||
/// Insert an entry in the database
|
||||
Future<void> _insertDB(T el) async {
|
||||
await (await DatabaseHelper.get()).insert(tableName(), el.toMap());
|
||||
await (await DatabaseHelper.get())!.insert(tableName(), el.toMap());
|
||||
}
|
||||
|
||||
/// Update an element in the database
|
||||
Future<void> _updateDB(T el) async {
|
||||
await (await DatabaseHelper.get()).update(
|
||||
await (await DatabaseHelper.get())!.update(
|
||||
tableName(),
|
||||
el.toMap(),
|
||||
where: "${BaseTableContract.C_ID} = ?",
|
||||
@ -34,14 +34,14 @@ abstract class ModelDatabaseHelper<T extends CacheModel> {
|
||||
/// Get an element from the database with a specified [id]
|
||||
///
|
||||
/// Returns null if none found
|
||||
Future<T> get(int id) async {
|
||||
List<Map> maps = await (await DatabaseHelper.get()).query(
|
||||
Future<T?> get(int id) async {
|
||||
List<Map> maps = await (await DatabaseHelper.get())!.query(
|
||||
tableName(),
|
||||
where: '${BaseTableContract.C_ID} = ?',
|
||||
whereArgs: [id],
|
||||
);
|
||||
|
||||
if (maps.length > 0) return initializeFromMap(maps[0]);
|
||||
if (maps.length > 0) return initializeFromMap(maps[0] as Map<String, dynamic>);
|
||||
|
||||
return null;
|
||||
}
|
||||
@ -50,7 +50,7 @@ abstract class ModelDatabaseHelper<T extends CacheModel> {
|
||||
///
|
||||
/// Return true if at least one entry was deleted / false else
|
||||
Future<bool> delete(int id) async {
|
||||
return await (await DatabaseHelper.get()).delete(
|
||||
return await (await DatabaseHelper.get())!.delete(
|
||||
tableName(),
|
||||
where: '${BaseTableContract.C_ID} = ?',
|
||||
whereArgs: [id],
|
||||
@ -59,22 +59,22 @@ abstract class ModelDatabaseHelper<T extends CacheModel> {
|
||||
|
||||
/// Get all the entries from the table
|
||||
Future<List<T>> getAll() async {
|
||||
List<Map> maps = await (await DatabaseHelper.get()).query(tableName());
|
||||
return maps.map((f) => initializeFromMap(f)).toList();
|
||||
List<Map> maps = await (await DatabaseHelper.get())!.query(tableName());
|
||||
return maps.map((f) => initializeFromMap(f as Map<String, dynamic>)).toList();
|
||||
}
|
||||
|
||||
/// Get some entries from the table based on some conditions
|
||||
Future<List<T>> getMultiple(
|
||||
{bool distinct,
|
||||
List<String> columns,
|
||||
String where,
|
||||
List<dynamic> whereArgs,
|
||||
String groupBy,
|
||||
String having,
|
||||
String orderBy,
|
||||
int limit,
|
||||
int offset}) async {
|
||||
List<Map> maps = await (await DatabaseHelper.get()).query(
|
||||
{bool? distinct,
|
||||
List<String>? columns,
|
||||
String? where,
|
||||
List<dynamic>? whereArgs,
|
||||
String? groupBy,
|
||||
String? having,
|
||||
String? orderBy,
|
||||
int? limit,
|
||||
int? offset}) async {
|
||||
List<Map> maps = await (await DatabaseHelper.get())!.query(
|
||||
tableName(),
|
||||
distinct: distinct,
|
||||
columns: columns,
|
||||
@ -86,12 +86,12 @@ abstract class ModelDatabaseHelper<T extends CacheModel> {
|
||||
limit: limit,
|
||||
offset: offset,
|
||||
);
|
||||
return maps.map((f) => initializeFromMap(f)).toList();
|
||||
return maps.map((f) => initializeFromMap(f as Map<String, dynamic>)).toList();
|
||||
}
|
||||
|
||||
/// Empty the table
|
||||
Future<void> clearTable() async {
|
||||
await (await DatabaseHelper.get()).execute("DELETE FROM ${tableName()}");
|
||||
await (await DatabaseHelper.get())!.execute("DELETE FROM ${tableName()}");
|
||||
}
|
||||
|
||||
/// Check out whether an element specified with its [id] is present
|
||||
|
Reference in New Issue
Block a user