fetchDoctorsId method
Fetches doctor documents from Appwrite and updates the state.
Implementation
Future<void> fetchDoctorsId() async {
emit(state.copyWith(isLoading: true, error: null));
try {
final List<String> queries = [Query.equal('type', 'doctor')];
if (state.fromDate != null) {
queries.add(
Query.greaterThanEqual(
'createdOn',
state.fromDate!.toIso8601String(),
),
);
}
if (state.tillDate != null) {
final tillEnd = DateTime(
state.tillDate!.year,
state.tillDate!.month,
state.tillDate!.day,
23,
59,
59,
);
queries.add(
Query.lessThanEqual('createdOn', tillEnd.toIso8601String()),
);
}
final response = await db.listDocuments(
databaseId: AppConstants.appwriteDatabaseId,
collectionId: AppConstants.userCollectionId,
queries: queries,
);
final List<models.Document> enrichedDocs = [];
for (final doc in response.documents) {
log(doc.data.toString());
final doctorName = doc.data['doctorName'] ?? 'Unknown';
final mothers = await db.listDocuments(
databaseId: AppConstants.appwriteDatabaseId,
collectionId: AppConstants.userCollectionId,
queries: [
Query.equal('doctorName', doctorName),
Query.equal('type', 'mother'),
],
);
final tests = await db.listDocuments(
databaseId: AppConstants.appwriteDatabaseId,
collectionId: AppConstants.testsCollectionId,
queries: [Query.equal('doctorName', doctorName)],
);
final updatedData =
Map<String, dynamic>.from(doc.data)
..['noOfMother'] = mothers.total
..['noOfTests'] = tests.total;
enrichedDocs.add(
models.Document(
$id: doc.$id,
$collectionId: doc.$collectionId,
$databaseId: doc.$databaseId,
$createdAt: doc.$createdAt,
$updatedAt: doc.$updatedAt,
data: updatedData,
$permissions: [],
),
);
}
emit(
state.copyWith(
allDoctors: enrichedDocs,
filteredDoctors: enrichedDocs,
isLoading: false,
),
);
} catch (e) {
emit(state.copyWith(isLoading: false, error: e.toString()));
}
}