Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

RangeError: Invalid value: Not in range 0..1, inclusive: -1 #38

Open
gilvangobbato opened this issue Aug 22, 2019 · 3 comments
Open

RangeError: Invalid value: Not in range 0..1, inclusive: -1 #38

gilvangobbato opened this issue Aug 22, 2019 · 3 comments

Comments

@gilvangobbato
Copy link

Hello,

I'm getting this error in my MoneyMaskedTextController with precision: 2.

Screenshot_2019-08-22-09-40-45-03

The error occurs when I go out of the TextField with less than 2 digits, for example: my mask is from Brazilian real R$ 0,00, if I type only 5 cents and go out of the TextFiel, I get this error.

For example:

Screenshot_2019-08-22-09-50-05-21

In this example, when I press the ritgh button, the error occurs.
My controller:

  MoneyMaskedTextController controller = new MoneyMaskedTextController(
      decimalSeparator: ',',
      thousandSeparator: '.',
      leftSymbol: 'R\$ ',
      precision: 2);

Debuging, the error appears occurs in this point on the "flutter_masked_text.dart":

  double get numberValue {
    List<String> parts = _getOnlyNumbers(this.text).split('').toList(growable: true);

    parts.insert(parts.length - precision, '.');

    return double.parse(parts.join());
  }

I think that is because the parts.length - precision = -1, so, the throw occurs

Thanks

@gilvangobbato gilvangobbato changed the title RangeError: Invalid value: Not in range 0..1, inclusive: -1 RangeError: Invalid value: Not in range 0..1, inclusive: -1 #bug Aug 22, 2019
@gilvangobbato gilvangobbato changed the title RangeError: Invalid value: Not in range 0..1, inclusive: -1 #bug RangeError: Invalid value: Not in range 0..1, inclusive: -1 [bug] Aug 22, 2019
@gilvangobbato gilvangobbato changed the title RangeError: Invalid value: Not in range 0..1, inclusive: -1 [bug] RangeError: Invalid value: Not in range 0..1, inclusive: -1 Aug 22, 2019
@x4080
Copy link

x4080 commented Jan 15, 2020

Hi @gilvangobbato, I think the solution is to add condition :
from

parts.insert(parts.length - precision, '.');

to

if (parts.length >= precision) parts.insert(parts.length - precision, decimalSeparator);

@benhurott maybe you can update it in the widget ?

@gilvangobbato
Copy link
Author

Hi @gilvangobbato, I think the solution is to add condition :
from

parts.insert(parts.length - precision, '.');

to

if (parts.length >= precision) parts.insert(parts.length - precision, decimalSeparator);

@benhurott maybe you can update it in the widget ?

Correct
I extended this class to update exactly this line. something like this:

  double get numberValue {
    List<String> parts =
        _getOnlyNumbers(this.text).split('').toList(growable: true);

    if (parts.length < precision) {
      for (var i = 1; i < precision; i++) {
        parts.insert(0, "0");
      }
    }
    parts.insert(parts.length - precision, '.');

    return double.parse(parts.join());
  }

@Miko2x
Copy link

Miko2x commented Aug 3, 2020

Replace to this

updateValue(double value) {
  double valueToUse = value;

  if (value != 0.0) {
    if (value.toStringAsFixed(0).length > 12) {
      valueToUse = _lastValue;
    } else {
      _lastValue = value;
    }

    String masked = this._applyMask(valueToUse);

    if (rightSymbol.length > 0) {
      masked += rightSymbol;
    }

    if (leftSymbol.length > 0) {
      masked = leftSymbol + masked;
    }

    if (masked != this.text) {
      this.text = masked;

      var cursorPosition = super.text.length - this.rightSymbol.length;
      this.selection = TextSelection.fromPosition(TextPosition(offset: cursorPosition));
    }
  }
}

double get numberValue {
  List<String> parts = _getOnlyNumbers(this.text).split('').toList(growable: true);

  if (parts.length != 0) {
    parts.insert(parts.length - precision, '.');
  } else {
    parts.insert(0, "0");
  }

  return double.parse(parts.join());
}

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants