Asri
Asri

Reputation: 325

Why doesnt my void function update my int variable?

I have 3 int variable that I want to update via a void function during an initstate. I tried printing them out and the value is correct but when I try to display them in my container, it still shows 0.

int equipmentCount1 = 0;
int equipmentCount2 = 0;
int equipmentCount3 = 0;

 @override
  void initState() {
    getEquipmentCount('Hammer', equipmentCount1);
    getEquipmentCount('Spanner', equipmentCount2);
    getEquipmentCount('Screwdriver', equipmentCount3);
    super.initState();
  }

void getEquipmentCount(String type, int counter) async {
    await Firestore.instance
        .collection('Notes')
        .document('CarNotes')
        .collection('PM Overview')
        .document(type)
        .collection(type)
        .getDocuments()
        .then((QuerySnapshot snapshot) {
      setState(() {
        return counter = snapshot.documents.length;
      });
    });
    print(counter);
  }

@override
  Widget build(BuildContext context) {
    return Scaffold(
body: Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
Text(equipmentCount1),
Text(equipmentCount2),
Text(equipmentCount3),


Upvotes: 0

Views: 116

Answers (2)

Peter Haddad
Peter Haddad

Reputation: 80924

The value is still 0, because in your method you are only changing the counter variable inside the method and not the instance variables equipmentCount3. You can create a list to add all the 3 values, and then use that list inside the build method:

int equipmentCount1 = 0;
int equipmentCount2 = 0;
int equipmentCount3 = 0;
List<int> listOfEquipments = List();

void getEquipmentCount(String type, int counter) async {
    await Firestore.instance
        .collection('Notes')
        .document('CarNotes')
        .collection('PM Overview')
        .document(type)
        .collection(type)
        .getDocuments()
        .then((QuerySnapshot snapshot) {
      setState(() {
        listOfEquipments.add(snapshot.documents.length);
      });
    });
  }

To add the list to the build method check the following:

https://stackoverflow.com/a/61548797/7015400

Upvotes: 1

dumazy
dumazy

Reputation: 14435

You should pass in a callback function so the state can be updated. Here's an example:

 @override
  void initState() {
    getEquipmentCount('Hammer', (int count) => setState(() { 
      equipmentCount1 = count;
    }));
    // same for the others
    super.initState();
  }

void getEquipmentCount(String type, ValueChanged<int> onCountChanged) {
    Firestore.instance
        .collection('Notes')
        .document('CarNotes')
        .collection('PM Overview')
        .document(type)
        .collection(type)
        .getDocuments()
        .then((QuerySnapshot snapshot) {
          onCountChanged(snapshot.documents.length);
        });
    });
    print(counter);
  }

Also, await isn't necessary since you're using then on the Future, and the function inside setState doesn't need to return anything.

Upvotes: 1

Related Questions