class StudentValidationMixin{
String validateFirstName(String value){
if(value.length<2){
return "Name must be at least two characters";
}
}
}
I am trying to use the feature of the container function and when I come to the validator part, I cannot run the code I mentioned above. I'm facing a problem as I wrote below
The body might complete normally, causing 'null' to be returned, but the return type, 'String', is a potentially non-nullable type.
body: Container(
margin: EdgeInsets.all(20.0),
child: Form(
child: Column(
children: [
TextFormField(
decoration: InputDecoration(labelText:"Öğrenci Adı",hintText: "Engin"),
validator: validateFirstName,
onSaved: (String value){
student.firstName=value;
},
naturally I get an error in this (validator: validateFirstName) part as well and this is the error I get
The argument type 'String Function(String)' can't be assigned to the parameter type 'String? Function(String?)?'.
I couldn't find this answer anywhere but I know the answer is hidden somewhere, I finally gave up and wanted to ask here. How do I fix this error? The trainer I learned Flutter can run this function without having these problems.
CodePudding user response:
As you rightly said the answer is hidden somewhere, indeed it is.
Problem:
The body might complete normally, causing 'null' to be returned, but the return
type, 'String', is a potentially non-nullable type.
The argument type 'String Function(String)' can't be assigned to the parameter type
'String? Function(String?)?'.
Reason:
I don't know about "Jack of all Trade" VSC, but, in Android studio, when you hover the cursor over any paramter, a pop-up tells you what type of input it takes.
So, for the paramter validator
in TextFormField
, the type acceptable in new Flutter versions which support Null Safety is String? Function(String?)?
as you can see in the below image:
But, it worked in the (old) video you watched because in versions lower than Flutter 2.0 (Null Safety), the type was String Function(String)
as you can see in the image below:
Now, you can figure out the error you're getting is indeed related to versions. Because, you're trying to use the old type in the new version as with this function:
class StudentValidationMixin{
String validateFirstName(String value){
if(value.length<2){
return "Name must be at least two characters";
}
}
}
Solution:
Change your validator()
type as below:
class StudentValidationMixin{
String? validateFirstName(String? value) {
if(value.length<2){
return "Name must be at least two characters";
} else {
return null;
}
}
}
See, what we did there? We changed the return type to nullable
, which means the value can be null and it should be in case when the validator validates the string correctly. As what it returns is the error in validation, and when there's no error, null should be returned.
Adding the ?
mark means telling the compiler that the particular variable's value can be null and of type given. For example The value of int? count
can be either integer or null, whereas the value of int count
can only be an integer
and will produce NullPointerException
if it isn't an integer
.
Also, comes with NullSafety is the exclaimation mark !
which tells the compiler that even when the variable was declared nullable, at this point, the value is not null. For ex. variable declared as int? count
, value entered into it and when you use it. use it as count!
to tell the compiler, at this point, there's a value in it.