I already implemented sqflite, the data Model and the default data. Also when I start the app, I receive all the single data entries from the db in a list.
What I want to do?! I want to start the app with default "settings". They (each single setting) should be read at the beginning and placed in an variable of the class "settings". This object will be used in the appContext, to make them available at every time.
My question is: How can I map the List from the database (setting_items) to the single settings in the object "settings"?
I'm a little bit familiar with databases, but not with data models and how to integrate them. Do you have any best practices or ideas how this is solved in a good way? Even if I'm totally wrong, please let me know. Honestly I'm not sure what to search for on google :)
many Thanks.
My Model:
class SettingItem {
final String id;
final String name;
final String family;
final String type;
final String? defaultValue;
final String? value;
SettingItem({
required this.id,
required this.name,
required this.family,
required this.type,
required this.defaultValue,
this.value = '',
});
SettingItem copyWith({
String? id,
String? name,
String? family,
String? type,
String? defaultValue,
String? value,
}) {
return SettingItem(
id: id ?? this.id,
name: name ?? this.name,
family: family ?? this.family,
type: type ?? this.type,
defaultValue: defaultValue ?? this.defaultValue,
value: value ?? this.value,
);
}
Map<String, dynamic> toMap() {
return {
'id': id,
'name': name,
'family': family,
'type': type,
'defaultValue': defaultValue,
'value': value,
};
}
factory SettingItem.fromMap(Map<String, dynamic> map) {
return SettingItem(
id: map['id'],
name: map['name'],
family: map['family'],
type: map['type'],
defaultValue: map['defaultValue'],
value: map['value'],
);
}
String toJson() => json.encode(toMap());
factory SettingItem.fromJson(String source) => SettingItem.fromMap(json.decode(source));
@override
String toString() => 'SettingItem(id: $id, name: $name, family: $family, type: $type, defaultValue: $defaultValue, value: $value)';
@override
bool operator == (Object other) {
if (identical(this, other)) return true;
return other is SettingItem &&
other.id == id &&
other.name == name &&
other.family == family &&
other.type == type &&
other.defaultValue == defaultValue &&
other.value == value;
}
@override
int get hasCode => id.hashCode ^ name.hashCode ^ value.hashCode;
}
Database Helper:
class DatabaseHelper {
//Singleton Pattern - one unique instance
DatabaseHelper._privateConstructor();
static final DatabaseHelper instance = DatabaseHelper._privateConstructor();
static Database? _database;
Future<Database> get database async => _database ??= await _initDatabase();
Future<Database> _initDatabase() async {
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, 'database.db');
return await openDatabase(
path,
version: 1,
onCreate: _onCreate,
);
}
_onCreate(Database db, int version) async {
await db.execute('''
CREATE TABLE setting_items(
id Text PRIMARY KEY,
name TEXT,
family TEXT,
type TEXT,
defaultValue TEXT,
value TEXT
)
''');
}
/******************* Settings *******************/
Future<List<SettingItem>> getSettingItems() async {
Database db = await instance.database;
var settingItems = await db.query('setting_items', orderBy: 'name');
List<SettingItem> settingItemsList =
settingItems.isNotEmpty ? settingItems.map((e) => SettingItem.fromMap(e)).toList() : [];
return settingItemsList;
}
Future<int> addSetting(SettingItem item) async {
Database db = await instance.database;
return await db.insert('setting_items', item.toMap());
}
Future<int> removeSetting(int id) async {
Database db = await instance.database;
return await db.delete('setting_items', where: 'id = ?', whereArgs: [id]);
}
Future<int> updateSetting(SettingItem item) async {
Database db = await instance.database;
return await db.update('setting_items', item.toMap(), where: 'id = ?', whereArgs: [item.id]);
}
}
Now the List should be loaded (on appstart) into the Settings to make them available.
class Settings {
final Setting appVersion;
final Setting language;
final Setting filteringActive;
Settings();
//How can I map the settings here? or what is the common way to do so?
}
many thanks again.