Reputation: 71
please what's the difference between a method called with a parameter and one called with no parameter example:
state is! LoginInProgress ? _onLoginButtonPressed : null,
and:
state is! LoginInProgress ? _onLoginButtonPressed(): null,
these two statements have different functionalities in my code execution, _onLoginButtonPressed and _onLoginButtonPressed() i mean, please I would really apricate if I can get the difference between the two, thanks in advance.
Upvotes: 0
Views: 153
Reputation: 1781
To be clear _onLoginButtonPressed
seems to be of type void Function()
and you trying to set callback to some widget (we have no full snippet - so I'm trying to guess)
Functions are objects in Dart - you can pass function as argument and call it later
This _onLoginButtonPressed
returns Function object - you not calling this function, you just send it elsewhere
When this _onLoginButtonPressed()
calls that function and returns void(nothing)
In second case you will get error when you try use result
This expression has a type of ‘void’ so its value can’t be used.
Upvotes: 0
Reputation: 3074
in this case, its not whether one of the methods has a parameter
or not but rather if you put the parenthesis
or not.
When you use the parenthesis
in a method, you are evoking it. (running the function).
state is! LoginInProgress ? _onLoginButtonPressed(): null,
<< runs the function
state is! LoginInProgress ? _onLoginButtonPressed : null,
<< doesn't run the function
to make it more clear:
dynamic storeFunction = _onLoginButtonPressed;
<< stores the function on the variable storeFunction
dynamic storeFunctionResult = _onLoginButtonPressed();
<< runs the function and stores the result returned by it on the storeFunctionResult
variable (if return type is not void
)
Upvotes: 0
Reputation: 11
When a method has a parameter and it requires a variable to perform the method thus its just like when your mom asks you to go out for shopping but you have arguments that you need money and a hello kitty bag to perform that task. For example : Let's say a method called a go_for_shopping requires two variables to perform its task, if we don't provide the integers it will give you an error.
void go_for_shopping(string hello_kitty_bag, int money){ return hello_kitty_bag + money;
}
But when you set no parameters it will perform the task without any arguments when you call that method.
Upvotes: 1