Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of curly braces in Dart method signature

class TapboxA extends StatefulWidget {

 TapboxA({Key key}) : super(key: key);

}

This part :

TapboxA({Key key}) 

The second half

super(key: key);

I understand, (a call to super class constructor).

But what is the

{Key key}

syntax doing?

like image 869
SirPaulMuaddib Avatar asked Oct 12 '17 20:10

SirPaulMuaddib


People also ask

What is the purpose of a curly brace?

Different programming languages have various ways to delineate the start and end points of a programming structure, such as a loop, method or conditional statement. For example, Java and C++ are often referred to as curly brace languages because curly braces are used to define the start and end of a code block.

What is the significance of curly braces in string handling?

As for me, curly braces serve as a substitution for concatenation, they are quicker to type and code looks cleaner.

What are the curly braces in API?

Curly braces are used to define "dictionary literals," giving you the ability to declare a dictionary and its contents inside your program. A dictionary literal consists of a series of key/value pairs, written with a colon (:) between them, and with each of the key/value pairs separated from one other with commas.

Which symbol is called as curly braces?

The symbols "{" and "} are called braces, curly braces or curly brackets.


3 Answers

It's not something special with constructors only.

For all Dart methods we have the option of named arguments as in languages like Python

Using a curly brace syntax, you can define optional parameters that have names.

So with the method signature using the curly braces,

TapboxA({Key key}) :

you can call this constructor in two different ways

The usual method, without named parameter

tapboxA1 = TapboxA(keyObject)

The extra nicety of named parameter

tapboxA2 = TapboxA(key: keyObject)

In other words the usefulness comes at the time of calling the method, not in the method itself.

like image 116
binithb Avatar answered Oct 25 '22 13:10

binithb


In Dart constructors ( and other methods ) can have optional named parameters :

MyClass({String namedParam}){//...}

In the case of a Flutter widget constructor :

TapboxA({Key key})  // TapboxA constructor defines a named parameters `key`
: super(key: key); //which is used within the super constructor call (which also has `key` as named parameter )

You can find more information about optional named parameters in the Dart language tour

like image 6
RX Labz Avatar answered Oct 25 '22 12:10

RX Labz


Dart provides an option on constructors. By default, when you instantiate a class with a constructor you are forced to provide the parameters defined.

Therefore, TapboxA({Key key}) means you can instantiate the class without providing the arguments.

like image 3
james kandau Avatar answered Oct 25 '22 11:10

james kandau