What is the difference between Function() and Function in dart?

442
  • Function represents any function:
void function() {}
int anotherFunction(int positional, {String named}) {}


Function example = function; // works
example = anotherFunction; // works too
  • Function() represents a function with no parameter:
void function() {}
int anotherFunction(int positional, {String named}) {}


Function() example = function; // works
example = anotherFunction; // doesn't compile. anotherFunction has parameters

A variant of Function() could be:

void Function() example;

Similarly, we can specify parameters for our function:

void function() {}
int anotherFunction(int positional, {String named}) {}

int Function(int, {String named}) example;

example = function; // Doesn't work, function doesn't match the type defined
example = anotherFunction; // works
Share:
442
erluxman
Author by

erluxman

Open for remote jobs You can also hire me through upwork https://wwwerluxman.com https://www.upwork.com/freelancers/~01ca1c56b1ce85b4dd

Updated on December 21, 2022

Comments

  • erluxman
    erluxman over 1 year

    While declaring a Function member in a class we can do both;

    Function first;
    Function() second;
    

    What is the difference between them?