flutter-masked-text icon indicating copy to clipboard operation
flutter-masked-text copied to clipboard

RangeError was thrown while dispatching notifications for MoneyMaskedTextController

Open cassioseffrin opened this issue 5 years ago • 4 comments

I am getting this exception when the MoneyMaskedTextController is set to blank ("")

flutter --version Flutter 1.20.2 • channel stable • https://github.com/flutter/flutter.git Framework • revision bbfbf1770c (11 days ago) • 2020-08-13 08:33:09 -0700 Engine • revision 9d5b21729f Tools • Dart 2.9.1

flutter_masked_text: 0.8.0

Here is the initializer _myMoneyField = MoneyMaskedTextController( decimalSeparator: ',', thousandSeparator: '.', precision: 3, initialValue: 1);

══╡ EXCEPTION CAUGHT BY FOUNDATION LIBRARY ╞════════════════════════════════════════════════════════ The following RangeError was thrown while dispatching notifications for MoneyMaskedTextController: Invalid value: Only valid value is 0: -2 When the exception was thrown, this was the stack:

(elided 3 frames from dart:async) The MoneyMaskedTextController sending notification was: MoneyMaskedTextController#6f686(TextEditingValue(text: ┤├, selection: TextSelection(baseOffset: -1, extentOffset: -1, affinity: TextAffinity.downstream, isDirectional: false), composing: TextRange(start: -1, end: -1)))

cassioseffrin avatar Aug 24 '20 12:08 cassioseffrin

Did you manage to solve?

denyjav avatar Sep 25 '20 02:09 denyjav

Eu resolvi.

O erro acima parou de ser lançado ao mudar o código fonte do widget. O método numberValue estava assim:

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

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

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

e alterei de forma que ficasse assim:

double get numberValue => double.parse(_getSanitizedText(this.text)) / 100.0;

Foi preciso também, adicionar o método _getSanitizedText:

String _getSanitizedText(String text) { String cleanedText = text;

var valuesToSanitize = [this.thousandSeparator, this.decimalSeparator];

valuesToSanitize.forEach((val) { cleanedText = cleanedText.replaceAll(val, ''); });

cleanedText = _getOnlyNumbers(cleanedText);

return cleanedText;

denyjav avatar Sep 26 '20 23:09 denyjav

The error was been caused when I have changed the text of MoneyMaskedTextController to blank text. _precoController.text = '';

I switch to: _precoController.updateValue(0);

The error have disappeared. The downside is when I update the value "updateValue(0)" I get the mask 0,00 on the instead of blank TextField. I don't found a way to clean/reset the TextFiled with (X) suffix icon to reset my TextField.

cassioseffrin avatar Sep 28 '20 12:09 cassioseffrin

Following this as well, would really like a way to be able to call textController.clear() without getting this error while using the MoneyMaskedTextController.

lampemw avatar Sep 30 '20 21:09 lampemw