'Assign sqlite query result to Flutter variable
My app requires employees to scan an NFC tag to clock in and out for work. What I want to achieve is to prevent duplicate entries - i.e. before an someone can clock in again, they have to be clocked out. The result is displayed in a snackbar.
Code for my conditional statement (in attendance.dart file):
var dataCheck = _query(tagIdentifier!);
if (dataCheck == 'Clock in') {
snackbarText = 'Already clocked in';
} else {
await addAttendanceData(
tStamp!,
scanDate!,
tagText!,
tagIdentifier,
tagAction,
workerText!,
attendanceFlag);
data = await ProjectDatabase.instance.attendanceReadAllData();
}
The _query code (in attendance.dart file):
_query(action) async {
duplicateCheck.add(Data.attendanceFromJson(action));
}
Then the code in the helper class (separate file).
Future<Data> readAttendanceData(String id) async {
final db = await instance.database;
final maps = await db.query(
attendanceDataTable,
columns: AttendanceDataFields.values,
where: 'tag_id = ?',
whereArgs: [id],
);
if (maps.isNotEmpty) {
return Data.attendanceFromJson(maps.first);
} else {
throw Exception('ID $id not found');
}
}
I have tried solving this issue by printing the dataCheck variable and got
Instance of 'Future'
Followed by the error:
type 'String' is not a subtype of type 'Map<String, Object?>'
My code structure was gathered from Johannes Milke's tutorial on the sqflite package in his note app example.
Any help will be greatly appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
