Reputation: 11
I have made an app in flutter that saves my user's data in Firestore with Flutter. I have made a collection called "Result" which has multiple documents, each with a document having the result of "Normal" or "COVID". What I want to do is get a count of all people with the result = COVID and display it in my flutter application.
CollectionReference reference= (await FirebaseFirestore.instance.collection('Result').add({
"date" :Timestamp.now().millisecondsSinceEpoch.toString(),
"result": _results[0]["label"],
})) as CollectionReference<Object?>;
In short, I expect something like this:
Below code prints, Records are 3
void Result() async{
var query = FirebaseFirestore.instance.collection("Result");
var snapshot = await query.get();
var count = snapshot.size;
print(' Records are $count'); //Records are 3
}
I want Result function to display this count when I press Generate Report. Hope so now I'm a bit clear.
enter code hereimport 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
class GenerateReport extends StatefulWidget {
const GenerateReport({Key? key}) : super(key: key);
@override
State<GenerateReport> createState() => _GenerateReportState();
}
class _GenerateReportState extends State<GenerateReport> {
//final fireStore= FirebaseFirestore.instance.collection('Result').where('result' , isEqualTo: 'COVID').count('result').snapshots();
final fireStore= FirebaseFirestore.instance.collection('Result').snapshots();
Result() async{
var query = await FirebaseFirestore.instance.collection("Result").where('result' , isEqualTo: 'COVID');
var snapshot = await query.get();
var count = snapshot.size; //return count;
print ('RESULTS ARE $count');
}
/* print(' Records are $count'); return Text ('Normal : $count',);//Records are 3 Future<Text> Result() async{*/
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
centerTitle: true,
title: const Text('Generate Report'),
flexibleSpace: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
begin: FractionalOffset.topCenter,
end: FractionalOffset.bottomCenter,
colors: <Color>[
Color.fromRGBO(47, 75, 104, 0.7),
Color.fromRGBO(47, 75, 104, 1)
]),
),
),
backgroundColor: Colors.transparent,
),
//centerTitle: true,
// backgroundColor: const Color.fromRGBO(47, 75, 104,1),
body: Center(
child: Column(
children: <Widget>[
const SizedBox(
height: 200,
),
ElevatedButton(
onPressed: Result,
child: const Text('Generate Report'),
/*async {
FirebaseFirestore.instance.collection('Result').get().then((QuerySnapshot querySnapshot) {
querySnapshot.docs.forEach((doc) {
print(doc["result"]);
});
});
}*/
),
//Result();
StreamBuilder<QuerySnapshot>(
stream: fireStore,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const CircularProgressIndicator();
}
if (snapshot.hasError) {
return const Text('Some error ');
}
return Expanded(
child: ListView.builder(
itemCount: snapshot.data!.docs.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(snapshot.data!.docs[index]['result'].toString()), //shows list of results
);
}),
);
}
),
],
),
),
);
}
}
Here I can see list of results not the exact count app
Upvotes: 1
Views: 619
Reputation: 9206
you need to aply a filter query with where
over that collection, use this:
void Result() async{
var query = FirebaseFirestore.instance.collection("Result").where("result", isEqualTo: "COVID");
var snapshot = await query.get();
var count = snapshot.size;
print(' Records are $count');
}
now it should get and print the count of the documents with a "COVID" value in the result
field.
Upvotes: 1