Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null safety #129

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/common/appstart/localization_initialize.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import 'package:kiwi/kiwi.dart';
/// correct language
///
class LocalizationInitialize {
PreferencesProvider _preferencesProvider;
String _languageCode;
PreferencesProvider? _preferencesProvider;
String? _languageCode;

///
/// Initialize the localization using the provided language code
Expand Down
40 changes: 19 additions & 21 deletions lib/common/data/database_access.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ import 'package:sqflite/sqflite.dart';

class DatabaseAccess {
static const String _databaseName = "Database.db";
static Database _databaseInstance;
static Database? _databaseInstance;

static const String idColumnName = "id";

Future<Database> get _database async {
if (_databaseInstance != null) return _databaseInstance;

_databaseInstance = await _initDatabase();
return _databaseInstance;
return _databaseInstance ??= await _initDatabase();
}

Future<Database> _initDatabase() async {
Expand Down Expand Up @@ -60,19 +57,20 @@ class DatabaseAccess {
}

Future<List<Map<String, dynamic>>> queryRows(String table,
{bool distinct,
List<String> columns,
String where,
List<dynamic> whereArgs,
String groupBy,
String having,
String orderBy,
int limit,
int offset}) async {
{bool? distinct,
List<String>? columns,
String? where,
List<dynamic>? whereArgs,
String? groupBy,
String? having,
String? orderBy,
int? limit,
int? offset}) async {
Database db = await _database;

// TODO: [Leptopoda] is there a reason this is done? or at maybe use whereArgs.removeWhere()
for (int i = 0; i < (whereArgs?.length ?? 0); i++) {
whereArgs[i] = whereArgs[i] ?? "";
whereArgs![i] = whereArgs[i] ?? "";
}

return await db.query(
Expand All @@ -89,33 +87,33 @@ class DatabaseAccess {
);
}

Future<int> queryRowCount(String table) async {
Future<int?> queryRowCount(String table) async {
Database db = await _database;
return Sqflite.firstIntValue(
await db.rawQuery('SELECT COUNT(*) FROM $table'));
}

Future<int> queryAggregator(String query, List<dynamic> arguments) async {
Future<int?> queryAggregator(String query, List<dynamic> arguments) async {
Database db = await _database;
return Sqflite.firstIntValue(await db.rawQuery(query, arguments));
}

Future<int> update(String table, Map<String, dynamic> row) async {
Database db = await _database;
int id = row[idColumnName];
int? id = row[idColumnName];
return await db
.update(table, row, where: '$idColumnName = ?', whereArgs: [id]);
}

Future<int> delete(String table, int id) async {
Future<int> delete(String table, int? id) async {
Database db = await _database;
return await db.delete(table, where: '$idColumnName = ?', whereArgs: [id]);
}

Future<int> deleteWhere(
String table, {
String where,
List<dynamic> whereArgs,
String? where,
List<dynamic>? whereArgs,
}) async {
Database db = await _database;
return await db.delete(
Expand Down
2 changes: 1 addition & 1 deletion lib/common/data/database_entity.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
abstract class DatabaseEntity {
Map<String, dynamic> toMap();
void fromMap(Map<String, dynamic> map);
fromMap(Map<String, dynamic> map);
}
7 changes: 4 additions & 3 deletions lib/common/data/database_path_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:io';

import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart';
import 'package:ios_app_group/ios_app_group.dart';
import 'package:app_group_directory/app_group_directory.dart';

Future<String> getDatabasePath(String databaseName) async {
Directory documentsDirectory = await getApplicationDocumentsDirectory();
Expand All @@ -24,12 +24,13 @@ Future<String> _getiOSDatabasePathAndMigrate(
// it must be saved in a group shared between the app module and the widget
// module. "Migration" means that the database at the old path gets
// copied to the new path
assert(Platform.isIOS);

var groupDirectory = await IosAppGroup.getAppGroupDirectory(
Directory? groupDirectory = await AppGroupDirectory.getAppGroupDirectory(
'group.de.bennik2000.dhbwstudentapp',
);

var newPath = join(groupDirectory.path, databaseName);
var newPath = join(groupDirectory!.path, databaseName);

var migrateSuccess = await _migrateOldDatabase(oldPath, newPath);

Expand Down
7 changes: 1 addition & 6 deletions lib/common/data/preferences/app_theme_enum.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@

///
/// Enum which holds the possible themes the app can be displayed in
///
enum AppTheme {
Dark,
Light,
System
}
enum AppTheme { Dark, Light, System }
24 changes: 9 additions & 15 deletions lib/common/data/preferences/preferences_access.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,26 @@ class PreferencesAccess {
case int:
await prefs.setInt(key, value as int);
return;
default:
throw InvalidValueTypeException(T);
}

throw InvalidValueTypeException(T);
}

Future<T> get<T>(String key) async {
Future<T?> get<T>(String key) async {
final SharedPreferences prefs = await SharedPreferences.getInstance();

T value;

switch (T) {
case bool:
value = prefs.getBool(key) as T;
break;
return prefs.getBool(key) as T?;
case String:
value = prefs.getString(key) as T;
break;
return prefs.getString(key) as T?;
case double:
value = prefs.getDouble(key) as T;
break;
return prefs.getDouble(key) as T?;
case int:
value = prefs.getInt(key) as T;
break;
return prefs.getInt(key) as T?;
default:
return null;
}

return value;
}
}

Expand Down
Loading