Home > Back-end >  retrieve an element from a collection firebase flutter
retrieve an element from a collection firebase flutter

Time:10-03

im trying to retrieve an element from a collection in firebase for example i have a list of courses and i want to get MATH course from the list.

i did this to retrieve the hall collection but i dont know how to search and retrieve for one element only. please help

getDocumentData() async {
  CollectionReference _cat = FirebaseFirestore.instance.collection("Courses");
  QuerySnapshot querySnapshot = await _cat.get();
  final _docData = querySnapshot.docs.map((doc) => doc.data()).toList();
  print(_docData);
}

this is my database

enter image description here

CodePudding user response:

To only get the course where cName is MATH you can use a query (FlutterFire docs here and here):

CollectionReference _cat = FirebaseFirestore.instance.collection("Courses");
Query query = _cat.where("cName", isEqualTo: "MATH");
QuerySnapshot querySnapshot = await query.get();
final _docData = querySnapshot.docs.map((doc) => doc.data()).toList();
  • Related