Following is my code. I’m trying to get all the ‘Babies’ which are in documents:
class _HomePageeState extends State<HomePagee> {
String t_babies = getCount().toString();
}
Future getCount() async {
return FirebaseFirestore.instance.collection('Babies').snapshots().length;
}
Instead I get this error: instance of _future<int>
Here is my Database. I expect to get 2 counts:
2
Answers
You need to use await when getting Future values and also you should pass Future and the type
Future<int>
:and also get the method using await but inside and async function:
You should use
setState
to update the string , because the fetch takes time as it involves network.