0

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,

  • check this out please : https://stackoverflow.com/questions/71803614/difference-between-myfunction-myfunction-and-myfunction-call-in-dart-flutt – editix Sep 12 '22 at 18:14

4 Answers4

0

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

0

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.

0

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.

Rashid Wassan
  • 664
  • 5
  • 13
-1

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.