getDevice method
- String key
Retrieves the device details from the database using the scanned code.
Implementation
Future<void> getDevice(String key) async {
debugPrint('device code --> $key');
try {
// 1. Query devices where deviceCode == key
final result = await databases.listDocuments(
databaseId: AppConstants.appwriteDatabaseId,
collectionId: AppConstants.deviceCollectionId,
queries: [
Query.equal('deviceCode', key),
],
);
if (result.documents.isNotEmpty) {
final deviceDoc = result.documents.first;
final deviceData = deviceDoc.data;
debugPrint('getDevice - ${deviceData["deviceCode"]}');
final Map<String, dynamic> updateData = {
"organizationId": deviceData["organizationId"],
"organizationName": deviceData["hospitalName"],
};
// 2. Update user document
await databases.updateDocument(
databaseId: AppConstants.appwriteDatabaseId,
collectionId: AppConstants.userCollectionId,
documentId: doctor.documentId!,
data: updateData,
);
context.pushReplacement(AppRoutes.home, extra: widget.doctor!);
setState(() {
code = deviceData["deviceCode"];
});
} else {
debugPrint("No device found with code: $key");
showSnackbar("No device found with this code.");
}
} catch (e) {
debugPrint("Appwrite error in getDevice: $e");
showSnackbar("Something went wrong while fetching the device.");
}
}