Na classe TaskDao
incluí as colunas faltantes no banco e atualizei a classe:
static const String tableSql = '''
CREATE TABLE $_tableName(
$_id TEXT NOT NULL,
$_name TEXT,
$_difficulty INTEGER,
$_image TEXT,
$_taskLevel INTEGER,
$_taskProgressLevel INTEGER,
$_taskProgressIndicatorValue REAL,
PRIMARY KEY($_id)
);
''';
static const String _taskLevel = 'level';
static const String _taskProgressLevel = 'progress_level';
static const String _taskProgressIndicatorValue = 'progress_indcator_value';
List<Task> toList(List<Map<String, dynamic>> tasksMap) {
final List<Task> tasks = [];
for (Map<String, dynamic> row in tasksMap) {
final Task task = Task(
id: row[_id],
name: row[_name],
image: row[_image],
difficulty: row[_difficulty],
taskLevel: row[_taskLevel],
progressLevel: row[_taskProgressLevel],
progressIndicatorValue: row[_taskProgressIndicatorValue],
);
tasks.add(task);
}
return tasks;
}
Map<String, dynamic> toMap(Task task) {
return {
_id: task.id,
_name: task.name,
_image: task.image,
_difficulty: task.difficulty,
_taskLevel: task.taskLevel,
_taskProgressLevel: task.progressLevel,
_taskProgressIndicatorValue: task.progressIndicatorValue,
};
}
Atualizei a Task
nas variáveis de progresso, removendo a inicialização dos valores, obrigando a serem inicializados na construção e os métodos de calculo de progresso para manipular o banco:
Future calculateProgress() async {
if (isNotMaxProgress()) {
widget.taskLevel++;
await TaskDao().save(widget);
setState(() {});
}
if (isTimeToLevelUpProgress()) {
await progressLevelUp();
} else {
await updateProgress();
}
}
bool isNotMaxProgress() {
return widget.progressLevel <= 6 && widget.progressIndicatorValue < 1;
}
bool isTimeToLevelUpProgress() {
return widget.progressIndicatorValue.compareTo(1) == 0 &&
widget.progressLevel <= 5;
}
Future progressLevelUp() async {
widget.progressIndicatorValue = 0;
widget.taskLevel = 0;
widget.progressLevel++;
await TaskDao().save(widget);
setState(() {});
}
Future updateProgress() async {
if (widget.difficulty <= 0) {
widget.progressIndicatorValue = 1;
await TaskDao().save(widget);
setState(() {});
} else if (widget.progressIndicatorValue < 1) {
widget.progressIndicatorValue =
(widget.taskLevel / widget.difficulty) / 10;
await TaskDao().save(widget);
setState(() {});
}
}
}
continuarei na resposta do post....