I have an int putOffCount which should be set to 19 on button press. In reality the int is set to 19 momentarilly before returning to 0. I am working in Flutter with sqflite as a local database.
Button widget:
TextButton(
child: Text("Change"),
onPressed: () async {
entry.putOffCount = 19;
await _databaseService.updateMemo(entry);
setState(() {});
},
),
updateMemo method:
Future<void> updateMemo(Memo memo) async {
final db = await _databaseService.database;
await db
.update('memos', memo.toMap(), where: 'id = ?', whereArgs: [memo.id]);
}
Memo class:
import 'dart:convert';
import 'package:intl/intl.dart';
class Memo {
int? id;
String title;
String description;
DateTime? createdDate;
DateTime? targetDate;
int? putOffCount;
Memo({
this.id,
required this.title,
required this.description,
this.createdDate,
this.targetDate,
this.putOffCount,
});
Map<String, dynamic> toMap() {
String setCreatedDate = '';
String setTargetDate = '';
if (createdDate == null) {
String formatted =
DateFormat("yyyy-MM-dd hh:mm:ss").format(DateTime.now());
setCreatedDate = formatted;
} else {
setCreatedDate = DateFormat("yyyy-MM-dd hh:mm:ss").format(createdDate!);
}
if (targetDate == null) {
String formatted = DateFormat("yyyy-MM-dd hh:mm:ss")
.format(DateTime.now().add(const Duration(hours: 1)));
setTargetDate = formatted;
} else {
setTargetDate = DateFormat("yyyy-MM-dd hh:mm:ss").format(targetDate!);
}
int setCount;
if (putOffCount == null) {
setCount = 40;
} else {
setCount = putOffCount ?? 50;
}
return {
'title': title,
'description': description,
'createdDate': setCreatedDate,
'targetDate': setTargetDate,
'putOffCount': setCount,
};
}
factory Memo.fromMap(Map<String, dynamic> map) {
DateTime tempTargetDate;
if (map['targetDate'] != null) {
tempTargetDate = DateTime.parse(map['targetDate']);
} else {
tempTargetDate = DateTime.now().add(const Duration(hours: 1));
}
return Memo(
id: map['id']?.toInt() ?? 0,
title: map['title'] ?? '',
description: map['description'] ?? '',
createdDate: DateTime.parse(map['createdDate']),
targetDate: tempTargetDate,
putOffCount: map['putOffCount'] ?? 30,
);
}
String toJson() => json.encode(toMap());
factory Memo.fromJson(String source) => Memo.fromMap(json.decode(source));
// Implement toString to make it easier to see information about
// each dog when using the print statement.
@override
String toString() {
return 'Memo(id: $id, title: $title, description: $description, createdDate: $createdDate,targetDate: $targetDate, putOffCount: $putOffCount)';
}
}
I tried variations of *await * and .then() methods with no success. Any help would be very appreciated.