'update field of a document in firebase from a listview flutter
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(),
);
},
),
);
}
}
Solution 1:[1]
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'});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
