Single function to update multiple state variables in Flutter?

Viewed 29

I have multiple counters in my app and want to use a single increment function and a single decrement function for all of them. Is it possible to do that? So far I have tried passing the state variable as an argument to the function but presumably because of the local scope of the function variable the change is not reflected.

This is a inside a state class.

  int qty = 0;

  void _decrement(int qty) {
    if (qty > 0) {
      setState(() {
        --qty;
      });
    }
  }

  void _increment(int qty) {
    setState(() {
      ++qty;
    });
  }

() => _increment(qty) this is passed to the the onPressed parameter of an ElevatedButton.

0 Answers
Related