Flutter: Textformfield validator not working - flutter

I am using TextFormField in flutter. i want to validate the TextFormField that value should be less than 100 or it should be character 'a' alone
I tried
validator: (value) {
if (value.length == 0 ) {
return ('value is required!');
}
else if(value != "a" || value != "A" || int.parse(value) < 0.0 || int.parse(value) > 100 )
{
return ('value should between 0 to 100 if absent put "A"!');
}
},
But it not working.Anyone please help me. Thanks in advance
Regards,
Sathish

Remember to return null when the validator should not display an error.
if (value.isEmpty) return 'value is required';
if (value.toLowerCase() == 'a') return null;
var intValue = int.tryParse(value);
if (intValue == null) { // not a number
return 'value should be between 0-100 if absent put A';
} else {
return intValue > 100 ? 'value should be between 0-100' : null;
}

Related

What is the correct dart syntax?

How do I do this in dart syntax, how do you read it?
The value must be between 3 to 20 characters
Following does not work:
validator: (value) {
return value != null &&
value.length < 3 &&
value.length > 20
? "Title between 3 ~ 20 characters"
: null;
}
validator: (value) {
return value != null &&
value.length > 10 &&
value is int
? "Number to big"
: null;
},
Following does work but only for less than 3 chars:
validator: (value) {
return value != null &&
value.length < 3
? "Title between 3 ~ 20 characters"
: null;
}
value length > 3 and value length < 20 if it's true null(valid) else return message "Title between 3 ~ 20 characters"
validator: (value) {
return value!=null && value.length > 3 && value.length < 20
? null
: "Title between 3 ~ 20 characters";
},

How to check a group of text edit controllers to see if any of them are null?

I'd like to have a function that checks to see if any of the text edit controller fields are null.
I wrote this method below, but I wanted to see if there was a more elegant solution.
bool _nullfieldExists(){
//return a true if any of the textEditController fields are null
if (_textEditControllerNumberPlayers.text == null ){
return true;
}
else if (_textEditControllerSmallBlind.text == null ){
return true;
}
else if (_textEditControllerBigBlind.text == null ){
return true;
}
else if (_textEditControllerAnte.text == null ){
return true;
}
else if (_textEditControllerMyStack.text == null ){
return true;
}
else {
return false;
}
}

"receiver can be null" when it can't?

I just finished my nullsafe migration. I'm finding that wrapping code with a null check only sometimes removes the need for the nullcheck ! operator? E.g.,
class MyClass {
double divideBy4(double numerator) {
return numerator / 4;
}
double quarteredWorks(double? value) {
if (value != null)
return divideBy4(value); // <- no intellisense warning
else
return 0;
}
double quarteredDoesntWork(double? value) {
return divideBy4(value); // <- intellisense: "double? can't be assigned to double"
}
double? value;
double divideBy2() {
if (value != null)
return value / 2; // <- intellisense: "receiver can be null"
else
return .0;
}
}
EDIT
Changed my example to show an example of wrapping with a null check that works

Convert value to number format in Flutter input?

I have a problem with Flutter InputTextField. I'm developing an app like CashApp were you have a feature which you can send money to other people.
The problem is: I need to achieve a number format and allow only two decimals.
For example, if I enter:
"1000,25" -> It needs to transform to 1.000,25
"10000,25" -> It needs to transform to 10.000,25
And so on..
I've been using this code to reach the only two decimals part
import 'package:flutter/services.dart';
import 'dart:math' as math;
class DecimalTextInputFormatter extends TextInputFormatter {
DecimalTextInputFormatter({this.decimalRange, this.activatedNegativeValues})
: assert(decimalRange == null || decimalRange >= 0,
'DecimalTextInputFormatter declaretion error');
final int decimalRange;
final bool activatedNegativeValues;
#override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, // unused.
TextEditingValue newValue,
) {
TextSelection newSelection = newValue.selection;
String truncated = newValue.text;
if (newValue.text.contains(' ')) {
return oldValue;
}
if (newValue.text.isEmpty) {
return newValue;
} else if (double.tryParse(newValue.text) == null &&
!(newValue.text.length == 1 &&
(activatedNegativeValues == true ||
activatedNegativeValues == null) &&
newValue.text == '-')) {
return oldValue;
}
if (activatedNegativeValues == false &&
double.tryParse(newValue.text) < 0) {
return oldValue;
}
if (decimalRange != null) {
String value = newValue.text;
if (decimalRange == 0 && value.contains(".")) {
truncated = oldValue.text;
newSelection = oldValue.selection;
}
if (value.contains(".") &&
value.substring(value.indexOf(".") + 1).length > decimalRange) {
truncated = oldValue.text;
newSelection = oldValue.selection;
} else if (value == ".") {
truncated = "0.";
newSelection = newValue.selection.copyWith(
baseOffset: math.min(truncated.length, truncated.length + 1),
extentOffset: math.min(truncated.length, truncated.length + 1),
);
}
return TextEditingValue(
text: truncated,
selection: newSelection,
composing: TextRange.empty,
);
}
return newValue;
}
}
This is a response I found from this post
This code almost do the thing. It restrict the two decimals part, but doesn't have the NumberFormat part, and instead of 'commas' for the decimals, it uses 'dots'.
I want to swap the '.' for thousands and ',' for decimals. And add the NumberFormat too.
Is there any way I can achieve this?
Is this for locale Brazil? Then the number currency format of Brazil could be set in you DecimalTextInputFormatter class to achieve the same result.

How to add range validation for year in flutter

I've tried to add year range validation it must be between 1850 to the current year. i've tried but it's not working.
Here is the code I've tried
String validateestablishedyear(String value) {
var date = new DateTime.now();
int currentYear = date.year;
int userinputValue = 0;
print('currentYear = $currentYear');
print('input value = $value');
print('value = ${value is int}');
print('value = ${value is String}');
if (value is String && value.length == 0) {
return "Established Year is Required";
} else {
userinputValue = int.parse(value);
}
if (userinputValue < 1850 && userinputValue >= currentYear) {
return "Year must be between 1850 and $currentYear";
}
return null;
}
Just change your code as below,
if (userinputValue < 1850 || userinputValue >= currentYear) {
return "Year must be between 1850 and $currentYear";
}
You're logic is wrong. you should have use || instead of &&