Display Document count from Firebase in a Text Widget in Flutter Web

Viewed 205

I am working on an admin web which i wanna to display some data like total users or total sales at the dashboard. But after much findings, I am not able to display the data in a Text Widget.

This is the home.dart

int placeCount = 0;
  countDocuments() async {
    
    QuerySnapshot myDoc2 =
        await FirebaseFirestore.instance.collection('Places').get();
    List<DocumentSnapshot> myDocCount2 = myDoc2.docs;
    this.placeCount = myDocCount2.length;
    print(this.placeCount);
 
    // Count of Documents in Collection
  }

  @override
  void initState() {
    countDocuments();
    super.initState();
  }

//The way  of displaying the data in Text Widget
Text(this.placeCount),

The result is the count print at the console is correct, but the count data display at the text widget will be initialization number i did which was 0. Is it something wrong i did that the placeCount cannot grab the correct data count from the countDocuments() method? Someone please help me. Thanks a lot!

1 Answers

You have to update the satet of your variable, call

setState(() {
  this.placeCount = myDocCount2.length;
});

this should update the value

Related