error in Flutter The named parameter 'onChanged' is required, but there's no corresponding argument. Try adding the required argument - flutter

What is the problem? How can I solve it? My codes:

You should pass the onChanged argument in ApprovalQueuePage as it is required.Pass it like this:
//When calling approval queue leave everything as it is
//just replace ApprovalQueuePage() with
ApprovalQueuePage(onChanged: (String value){
//You can do anything with the value
})

Related

How to pass a reference to a function in RaisedButton.onpressed in FLUTTER, but with parameters?

I already understood that we should call functions in FLUTTER without parentheses because you want to establish a reference to that function, not execute it.
But what if you want to pass parameters to that function?
How can I tell FLUTTER that I want to reference the function (not execute it), but pass arguments to it?
The arguments are usually specified inside parentheses.
I tried calling a function with parentheses when I have parameters to indicate, and it seems to work perfectly! It only executes the function when the button is pressed.... I'm a little confused.
If the parameters line up you can pass a reference to the function and it will be called with those parameters.
String validate(String? value) {
if (value == null) {
return 'Required';
}
return null;
}
TextFormField(
// Signature of validator and validate are the same
validator: validate
)
If the signatures don't overlap you have to use an anonymous function.
TextFormField(
validator: (value) => someOtherFunction(),
)

Can't assign non-nullable type to a nullable one

error: The argument type 'Future<List<GalleryPictureInfo>>' can't be assigned to the parameter type 'Future<List<GalleryPictureInfo>>?'.
Is this Dart Analysis or me? The project still compiles.
Upd. Added code example
FutureBuilder<List<GalleryPictureInfo>>(
future: derpiService.getListOfImages(),
//other code
);
#override
Future<List<GalleryPictureInfo>> getListOfImages(arguments) async {
List<GalleryPictureInfo> listOfImages = [];
var searchImages = await getSearchImages(tags: tags, page: page);
//adding images to List
return listOfImages;
}
It's something with FutureBuilder actually. I should've mention this.
Upd. "Fixed" with // ignore: argument_type_not_assignable
Looks like a problem with Dart Analysis for now
Upd. Error
It actually is an error which is pretty self explanatory.
The acutal error comes because of null safety in dart.
For ex:
void main(){
var number = getNumber(true);
int parsedNumber = int.parse(number);
print(parsedNumber);
}
String? getNumber(boolean value) {
if (value){
return null;
} else return "1";
}
So here, getNumber function either returns null or "1" depending upon the value of value variable. So, number variable's type is String?.
But the error shall arise in the next line when you try to call int.parse(). int.parse function takes an argument which should be a String but the value passed in the function is of type String?. So if we pass null in int.parse it shall throw an error.
That's why Dart analysis makes it easier to identify such cases by telling us that the value can be null and it might throw.
However the code depends upon your actual code of your project. It says that you are passing Future<List<GalleryPictureInfo>>? which is of nullable type to a function which requires Future<List<GalleryPictureInfo>>. So, before passing the value you might want to check if the value you are passing is not null.
If you are sure that the value can never be null then if for ex: if you are passing a variable called value, you might wanna try someFunctionWhereYouPassValue(value!)
That ! means that you are sure that the value will never be null.
For more details about null safety you can see:
https://dart.dev/null-safety/understanding-null-safety

Unable to put function in validator in flutter

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.
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.

Method 'replaceFirst' cannot be called on 'String?'

Error: Method 'replaceFirst' cannot be called on 'String?' because it is potentially null.
Try calling using ?. instead.)
.replaceFirst(r'$selectedRowCount', formatDecimal(selectedRowCount));
As Saffron-codes says, you cant do a replaceFirst on a 'String?' variable since it can be null, and dart is null safe.
There's two options for you to do. You can either make the variable a 'String' instead, if you do this you'll have to give it a value when initiating it:
String variableName = ''
Instead of:
String? variableName
You could also do a null-check (adding a questionmark before .replaceFirst) when calling replaceFirst, this is what it suggests doing in the error message 'Try calling using ?. instead.':
variableName?.replaceFirst(r'$selectedRowCount', formatDecimal(selectedRowCount));
Add the ! operator before . to make it non-nullable
!.replaceFirst(r'$selectedRowCount', formatDecimal(selectedRowCount));

A way to read a String as dart code inside flutter?

I want to build a method to dynamically save attributes on a specific object
given the attribute name and the value to save I call the "save()" function to update the global targetObj
var targetObj = targetClass();
save(String attribute, String value){
targetObj.attribute = value;
print(targetObj.attribute);
}
But I'm getting the following error:
Class 'targetClass' has no instance setter 'attribute='.
Receiver: Instance of 'targetClass'
Tried calling: attribute="Foo"
The only thing that I can think of is that "attribute" due to being type String results in an error.
That lead me to think if there is a way to read a String as code, something like eval for php.
As #Randal mentioned, you cannot create class..method at runtime. Still, you can try something like this.
A certain class
class Foo {
dynamic bar1;
dynamic bar2;
// ...
}
Your save method
save(Foo fooObject, String attribute, dynamic value) {
if ("bar1" == attribute) fooObject.bar1 = value;
else if ("bar2" == attribute) fooObject.bar2 == value;
// ...
}
Dart (and thus flutter) does not have a way to compile and execute code at runtime (other than dart:mirrors, which is deprecated). You can build additional code that derives from other code using the various builder mechanisms, although it can be rather complicated to implement (and use!).