Trying To Convert String Into Currency Format Fails

I am trying to format the input entered by the user into a currency format where every 3 digits are separated by a comma eg. 25,636 or 123,456 using the flutter intl library, however I keep getting an error

Class ‘String’ has no instance getter ‘isNegative’.
Receiver: “22” (This is value I have entered)
Tried calling: isNegative

Here’s my code

    String value = priceTextController.text;
    if(value.length > 1) {
      priceTextController.text = NumberFormat.simpleCurrency(decimalDigits: 3, name: "", locale: "en_ZA").format(value); //This is the line where it fails
    }  
    if(value.length < 4 || value.length > 7){
      return "Invalid Price.";
    }
    else if(numberssOnlyRegExp.hasMatch(value))
      return 'Invalid Price.';

    return null;
  } ```

How do I resolve this?

The value you’re passing to format must be a number, since you’re expecting decimal number input from the textcontroller you can do a NumberFormat.simpleCurrency(decimalDigits: 3, name: “”, locale: “en_ZA”).format(double.tryParse(value));

2 Likes