1.fireStore data fetch in flutter app.
SizedBox(
height: 300,
width: 100,
child: StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection('isNotEv').snapshots(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return ListView.builder(
itemCount:snapshot.data!.docs.length,
itemBuilder: (context, index) {
return ListTile(
title: Text( snapshot.data!.docs[index]['farePerKm'].toString()),
);
},
);
} else if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else {
return const CircularProgressIndicator();
}
},
),
),
2. insert the data in firestore.
Future<void> addNewDocument() async {
try {
// Get the reference to the Firestore collection where you want to add the document
CollectionReference collectionReference = FirebaseFirestore.instance.collection('isEv');
// Add the document to the collection without specifying the document ID
DocumentReference documentReference = await collectionReference.add({
'field1': 'value1',
'field2': 'value2',
// Add more fields as needed
});
// Fetch the autogenerated document ID
String autogeneratedDocumentId = documentReference.id;
print('Autogenerated Document ID: $autogeneratedDocumentId');
} catch (e) {
print('Error adding document: $e');
}
}
3.fetch data in log
void getride() async {
//userModelCurrentInfo
log("Ride Requests");
QuerySnapshot querySnapshot =
await FirebaseFirestore.instance.collection("allUsers")
.doc("customer")
.collection("customers")
.get();
//sub-patch
for (int i = 0; i < querySnapshot.size; i++) {
var a = querySnapshot.docs[i];
String st="Rc5qR3t74kf02JwDICZD";
if ( st==a.id){
//log(st!);
log("firstName: ${a['firstName']}");
}
}
}