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?
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.
As for me, curly braces serve as a substitution for concatenation, they are quicker to type and code looks cleaner.
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.
The symbols "{" and "} are called braces, curly braces or curly brackets.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With