Home > Blockchain >  how to not return negative values when hit decrement value at 0
how to not return negative values when hit decrement value at 0

Time:05-10

I am trying to implement value which doesn't allow user to pass negative value as this button functioning to print pages.

class _QuantityCounterState extends State<QuantityCounter> {
  int _counter = 0;

  void increment() {  
    setState(() {
      _counter  ;
    });
  }

  void decrement() {  
    setState(() {
      _counter--;
    });
  }

CodePudding user response:

void decrement() {  
    setState(() {
      _counter=<0 ? _counter=0 : _counter--;
    });
  }

CodePudding user response:

More elegant solution:

import 'dart:math';
 

    void decrement() {  
        setState(() {
          _counter = max(_counter-1, 0);
        });
      }
  • Related