Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.4 kB
2
Indexable
Future<List<Article>> fetchArticles() async {
  List<Article> articles = [];
  QuerySnapshot<Map<String, dynamic>> snapshot =
      await FirebaseFirestore.instance.collection('Articles').get();

  for (QueryDocumentSnapshot<Map<String, dynamic>> doc in snapshot.docs) {
    String categorieRefPath = doc['categorie'].path; // Get the reference path
    String vendeurRefPath = doc['vendeur'].path; // Get the reference path

    // Fetch the referenced categorie and vendeur documents
    DocumentSnapshot<Map<String, dynamic>> categorieSnapshot =
        await FirebaseFirestore.instance.doc(categorieRefPath).get();
    DocumentSnapshot<Map<String, dynamic>> vendeurSnapshot =
        await FirebaseFirestore.instance.doc(vendeurRefPath).get();

    // Create the Article object and populate the references
    articles.add(Article(
      titre: doc['Titre'],
      prix: double.parse(doc['Prix'].toString()),
      statut: doc['Statut'],
      description: doc['Description'],
      categorie: categorieSnapshot.data()?['Nom'], // Replace 'Nom' with the actual field name in the categorie document
      dateMiseEnVente: (doc['Date de mise en vente'] as Timestamp).toDate(),
      etat: doc['Etat'], // Use the correct field name here
      photos: List<String>.from(doc['Photo']),
      vendeur: vendeurSnapshot.data()?['Nom'], // Replace 'Nom' with the actual field name in the vendeur document
    ));
  }

  return articles;
}