Gary Olsson
Gary Olsson

Reputation: 1217

Why is an object instantiated after its declaration?

Okay so I'm trying to learn Dart by following flutter tutorials.
In the example below, right after the object is declared, an instance of itself is "created" (or at least I think so) and I don't understand why.

class CounterDisplay extends StatelessWidget {
  CounterDisplay({this.count}); // What does this line do ?

  final int count;

  @override
  Widget build(BuildContext context) {
    return Text('Count: $count');
  }
}

This code is from the tutorial found on this page:
https://flutter.dev/docs/development/ui/widgets-intro#changing-widgets-in-response-to-input

The line in question is this one :
CounterDisplay({this.count});
Could someone explain to me what does this line do and why it's here?

Upvotes: 0

Views: 63

Answers (2)

Rémi Rousselet
Rémi Rousselet

Reputation: 277437

This doesn't create an instance of the object.

It is instead what we call a "constructor". Such syntax allows specifying custom parameters that need to be passed when creating the object.

See the dart documentation on constructors for more informations.

Upvotes: 3

abdulrahmanAbdullah
abdulrahmanAbdullah

Reputation: 333

This make argument optional when create new object, or pass the variable name when creating object .

Upvotes: 0

Related Questions