1

I want to show list of documents from Firestore ,

and also i want if i click the button accept , I tried to show all the documents in a listview but i have a problem with the button , i want that the statut of the document change from "en cours" to "accept" but i can't get the id of the document to update the data once the text is cliqued .



import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

import '../rounded_button.dart';

class DemandeList extends StatelessWidget {
  final db = FirebaseFirestore.instance;
        String? Key ; 
        
      DemandeList({this.Key});
     

  @override
  Widget build(BuildContext context) {
    
    return Scaffold(
      appBar: AppBar(
        title: Text("Listes des demandes"),
        centerTitle: true,
      ),
          
      body: StreamBuilder<QuerySnapshot>(
        stream: db.collection('ambulance')
        .where("etat", isEqualTo: "en cours")
        .snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return Center(
              
              child: CircularProgressIndicator(),
            );
          } else
            return ListView(
              children: snapshot.data!.docs.map((doc) {
                return Card(
                  child: ListTile(
                  trailing: Text("Accepter",
                           style: TextStyle(
                             color: Colors.green,fontSize: 15
                             ),
                             ),
                  title: new Text(doc['id']) ,
                  subtitle: new Text(doc['etat']),
                   
                   
                  onTap: () => {
                    
                  }
                  
              
                  ),
                  
                   
                );
              }).toList(),
            
              
            );
        },
        
      ),
    );
  }
}
nullonprog
  • 119
  • 1
  • 9

1 Answers1

0

I don't see methods inside onTap so I'm just assuming but if you want to access documentID you should write doc.id instead of doc["id"].

hope this is what you are seeking for.

for updating the document when text is tapped, you can write as below

onTap:()=>db.collection('ambulance').doc(doc.id).update({etat:'accept'});
Sho
  • 104
  • 5
  • Thank you for your response , I know there is no method inside onTap , and that because i don't know how to implement the methode . – nullonprog Mar 20 '22 at 14:43
  • thanks, i updated my answer with the code to which might be the answer for your onTap function – Sho Mar 25 '22 at 14:07