i have collection named “Categories” in firebase and under this collection i have documents that i want to fetch . i defined the methode “getAllCategories” that is supposed to get the Categories from the db :
final _db = FirebaseFirestore.instance;
Future<List<CategoryModel>> getAllCategories() async {
try {
final snapshot = await _db.collection('Categories').get();
final list =
snapshot.docs.map((e) => CategoryModel.fromSnapshot(e)).toList();
print("${list}");
return list;
} on FirebaseException catch (e) {
throw TFirebaseException(e.code).message;
} on PlatformException catch (e) {
throw TPlatformException(e.code).message;
} catch (e) {
throw 'Something went wrong.Please try again';
}
}
and then i called this function in the “fetchCategories” function :
Future<void> fetchCategories() async {
try {
isLoading.value = true;
final Categories = await _categoryRepository.getAllCategories();
allCategories.assignAll(Categories);
featuredCategories.assignAll(allCategories
.where((category) => category.isFeatured && category.parentId.isEmpty)
.take(8)
.toList());
} catch (e) {
TLoader.errorSnackBar(
title: 'Oh Snap mochkla fl caregori', message: e.toString());
} finally {
isLoading.value = false;
}
}
lass CategoryModel {
String id;
String name;
String image;
String parentId;
bool isFeatured;
CategoryModel(
{required this.id,
required this.name,
required this.image,
required this.isFeatured,
this.parentId = ''});
here is the category model:
there are no errors but after i did some tests i figured out that the method not working is the getAllCategories function.if anyone can give the possible reason for this problem he will save me days