Flutter. Dart. Null-aware callback call? [duplicate] - flutter

In the same way we can have
nullableClassInstance?.method(blah)
Is there a way to do
nullableFunctionInstance?(blah)
In other words, is there an operator that checks whether a function instance is not null, if so, invoke the function all in one line?

Using the call method, you can achieve what you want with:
nullableFunctionInstance?.call(blah)
There's also the apply method if you want to pass arguments.

If you have a Function Object , you can use the call method and send all the parameters to that which works exactly as calling the function. Here , you can use the null aware member access operator.
void myFun(int a , int b){...}
var myVar = myFun ;
call
The function myVar will only get called if its not null as shown below.
myVar?.call( arg1 , arg2 );
apply
If your function is dynamic or you wish to control which function is being called at run time , you can use the apply static method of Function like so :
Function.apply(myVar , [arg1 , arg2]);
apply takes the function and a List of parameters that will be sent to the function.
Read more about call and apply :

Related

Difference between Function and Function() in flutter [duplicate]

This question already has an answer here:
Function and Function()
(1 answer)
Closed 5 months ago.
I have a doubt where I'm passing a function as a parameter, for onPressed, like event, I would declare a field like final void Function() onPressed, There are some code where I have seen declarring fields like Function someFunction, I would like to know what is the differnce between using Function and Function(). Thanks,
The main() function came from Java-like languages so it's where all programs started, without it, you can't write any program on Flutter even without UI.
The runApp() function should return a widget that would be attached to the screen as a root of the widget Tree that will be rendered
As we can see in doc, Function is:
part of dart.core;
/// The base class for all function types.
///
/// The run-time type of a function object is subtype of a function type,
/// and as such, a subtype of [Function].
Now, Function(), is literally a function, see the example:
Here you can use this:
final Function() func = () {};
ElevatedButton(
onPressed: func;
)
But you can not:
final Function func = () {};
ElevatedButton(
onPressed: func;
)
Here you get the following error:
The argument type 'Function' can't be assigned to the parameter type 'void Function()?'.dartargument_type_not_assignable
Otherwise, you can use:
Function func() {
return () {};
}
ElevatedButton(
onPressed: func;
)
Use Function somename when you need to return a function from a method.
Use Function() somename when you need to use this, literally, as a function.
As per Flutter docs:
Function is the base class of all the function types. It means it represents all the functions.
Adding parenthesis like: Function() makes it one without any parameter.
To sum up: Function is general and Function() is more specific.
Yep, lets take a look whats is braces actualy means.
The main difference between Method(); and Method, is that in first case, you call a method instant and its all. If you'l try to do var someStuff = Method(), someStuff will get a returning value of Method();
In second case, when you use Method, you actually do not call it, because its a link to a method. Because of it is a link, you cant pass an arguments (which places in braces), and if you try to make set variable like this: var someStuff = Method;, variable someStuff will contains a link to a original Method(), and you can call it in code, and it'l work same.
That means, when you pass a link to a onPressed:, and if link has right signature (in and out param), you haven't write whats goes in method and out, all what you should to do will looks like: onPressed: Method. Dart automatically understand, what params in and out, by comparing theirs signature.

not able to use DISTINCT or group by keyword in codeigniter

This is my controller.php:
$courserecord = $this->front->get_data_wheree('tbl_course_offered.course_id',array('isactive'=>'1'));
This is my Model.php:
function get_data_wheree($table,$where)
{
return $this->db->group_by($table,$where)->result();
}
In my model.php I want to use DISTINCT OR GROUP_BY in the Query.
The group_by is not working properly.
Your model is not working because the result() method is a member of the class CI_DB_result but group_by() returns an instance of CI_DB_query_builder. In other words, you are trying to call a method that does not exist for the class being used.
The methods get() and get_where() are the only two Query Builder methods that return a CI_DB_result object. Put simply, you have to call get() or get_where() before you can use result() or any similar methods.
The other problem is that group_by() does not take a second argument of the type you provide. You're providing an array but it expects a boolean. You need to add another method to define a where condition.
Your model's method should look like the following.
I changed the name of the first argument to better describe its purpose.
function get_data_where($groupby, $where)
{
return $this->db
->group_by($groupby)
->where($where)
->get('tbl_course_offered')
->result();
}

Pre-apply parameter to unityscript function and use that as an object to send into another function

I have one complex function. I planned to send a function into it.
function ComplexFunction( customFunction : function)
{
//Complex things
customFunction();
//Complex things
}
But the function I planned to send has different signature
function FunctionA ( enumParameter : EnumX )
function FunctionB ( enumParameter : EnumY )
function FunctionC ( enumParameter : EnumZ )
So this is not an option because I don't know what type will be sending in.
function ComplexFunction( customFunction : function , enumForCustomFunction : Enum??? )
(This is Unity's unityscript with #pragma strict, so I must indicate the parameter type.)
So I think about pre-apply those enum parameter, make it into parameter-less function and send that in for ComplexFunction to call. Is this possible?
In plain JavaScript you can bind parameters to a function like this:
var multiply = function(x,y) { return x*y };
var doubleIt = multiply.bind(null, 2);
So calling doubleIt(3) now returns 6. (The null sets the context of the function, in this case we don't care about that, so we use null). You can read more on this topic here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
In your case I would call ComplexFunction with FunctionA like so:
ComplexFunction(FunctionA.bind(null, AValueFromEnumX));

Callback from python

After reading a lot from this wonderful web, I need some help.
I have this function (in a dll):
kvStatus kvSetNotifyCallback ( const int hnd, kvCallback_t callback, void * context,
unsigned int notifyFlags);
How to call this function from Python?
I tried:
kvSetNotifyCallback(c_int(hnd1),c_void_p(self.can_rx()),None, c_int(canNOTIFY_RX))
def can_rx(self):
print("OK")
The function can_rx does execute only once. Any suggestions? Thank you very much.
MM
first thing:
The argtypes of ctypes WINFUCNTYPE or CFUNCTYPE should be a c_void_p
The argtypes of fucntion which you are defining should be py_object instead of a c_void_p
so that you can pass objects of structures and arrays as well.
then in the callback fucntion cast the void_p data as
ct.cast(userdata,ct.py_object).value
second thing:
code running only once: put a return value at bottom of the code.
return 1 or return TRUE
example:
CALLBACKFUNC = WINFUNCTYPE("returnvalue",c_int,c_void_p)
kvSetNotifyCallback.argtypes = [c_int,CALLBACKFUNC,py_object,c_uint]
then in the fucntion:
def can_rx(arg1,arg2):
arg3 = cast(arg2,py_object).value #yields object value
print arg1
print arg3
return 1 #for continuing execution
The CALLBACK FUNCTION can_rx should be called at CALLBACKFUNC(can_rx), the data to be passed along with arg1 should be passed at py_object position.
so you should call your callback as:
kvSetNotifyCallback(somevalue,CALLBACKFUC(can_rx),arg2,canNOTIFY_RX)
arg2 is the value to be passed to the callback func can_rx
can_rx can be defined as a global function also.

Correct way to return function value instead of binding in Coffeescript

I can't seem to find a concise answer to this question. What is the correct coffeescriptic way to return the value from _otherInstanceMethod when calling #_instanceMethod instead of the function binding itself?
x = _instanceMethod: () ->
#_otherInstanceMethod key: 'value'
Edit (thanks commenters)
This returns:
x = function () {
[...] # function body omitted
});
Instead of
x = 'some value returned by _otherInstanceMethod'
I would like the value to be returned instead of the function binding to _otherInstanceMethod
Being totally new to Coffeescript, this was my fault. I was calling the instance method like:
#_instanceMethod
instead of
#_instanceMethod()
Sorry for the trouble, voting to delete
In CoffeeScript #something translated into this.something regardless of the underlying variable type. This means you can use # only in conjuction with properties, with methods you still ought to use good old this.