Cupertino Slider shows values of 499.99 instead of 500

169 Views Asked by At

So I want to build a slider where users can choose a value between 100-1000 and the value should be like this 100,150,200,250... 950,1000 but I noticed that I get values like 499.9999994 or 500.000001. Any idea how can I fix this?

 Row(
  mainAxisAlignment: MainAxisAlignment.center,
   children: [
    Text('100'),
    CupertinoSlider(
     value: context.watch<LoanValue>().value,
     min: 100,
     max: 1000,
     divisions: 18,
     onChanged: (double value) {
      context.read<LoanValue>().updateValue(value);
     },
    ),
     Text('1000'),
    ],
   ),

this is where I update the value:

class LoanValue with ChangeNotifier, DiagnosticableTreeMixin {
  double loanValue = 100;
  double get value => loanValue;

  void updateValue(double division) {
    loanValue = division;
    // debugPrint('loanValue: $loanValue');
    notifyListeners();
  }
}

My app page

1

There are 1 best solutions below

0
On

I fixed this by adding this .roundToDouble()

  void updateValue(double division) {
    loanValue = division.roundToDouble();
    // debugPrint('loanValue: $loanValue');
    notifyListeners();
  }