NM AA
NM AA

Reputation: 5

What does Function declared without curly braces means in Dart?

I saw a tutorial where he declared a function like this:

class Person {
    String name;
    Function(String name) doingHobby;
}

What does it mean? how is it differ with common Function with bracket? This also not even looks like arrow function.

Thanks.

Upvotes: 0

Views: 274

Answers (1)

julemand101
julemand101

Reputation: 31219

It means that doingHobby is a variable which is allowed to point to as function which returns dynamic (if we don't specify any return value, Dart will assume dynamic which basically means it is allowed to return anything including void) and takes one String argument.

Here is an example where I assign a void Function(String) to it using a constructor and later calls this function by using the doingHobby variable:

class Person {
  String name;
  void Function(String name) doingHobby;

  Person(this.name, this.doingHobby);
}

void main() {
  final person = Person(
    'Jakob',
    (hobby) => print('Doing $hobby'),
  );
  person.doingHobby('playing football'); // Doing playing football
}

Upvotes: 1

Related Questions