I would like the appBar title of my Scaffold to display the total number of items (length) in a Firebase Query at launch, but it keeps returning Instance of 'Future<int>'
. How can I fix this?
Here’s my code:
Query itemList = FirebaseFirestore.instance
.collection('simple');
Future<int> itemCount() async => FirebaseFirestore.instance
.collection('simple')
.snapshots()
.length;
...
return Scaffold(
appBar: AppBar(
title: Text("# Items: " + itemCount().toString()),
// DISPLAYS: # Items: Instance of 'Future<int>'
Unfortunately, it displays Instance of 'Future<int>'
. What do I have to change to obtain the item count (length) and have that show-up in the title text?
Thank you!
3
Answers
After some experimenting, I came to the conclusion that I need to use StreamBuilder. Here's the solution that fixed it:
Screenshot of completed project with # Items in appBar title:
The floating action button adds items to the list and onTap deletes the selected item from the list. The title remains updated.
I hope this helps someone in the future.
You can use a FutureBuilder like this :
You are calling a "Future" function, thats the function return a Future so you cant display it like that, you need to use an await (if you are in async function) or a
.then()
(if you’r not in async function).The best way to print it in your case is to use a FutureBuilder or the keyword await.