Reputation: 1164
I am doing a course on Flutter from Udacity. For some assignment, I am learning how to create widgets. Coming from a C++/Python, I cannot understand this class constructor syntax at all.
So my main.dart contains a Category Widget (the one I am building), inside a Center Widget. I am passing 3 parameters from the main.dart file, but I don't understand what the
const Category({...}) : ... ;
part is doing.
Here is how my category.dart looks like:
import 'package:flutter/material.dart';
class Category extends StatelessWidget {
final String name;
final ColorSwatch color;
final IconData iconLocation;
const Category({
Key key,
@required this.name,
@required this.color,
@required this.iconLocation,
}) : assert(name != null),
assert(color != null),
assert(iconLocation != null),
super(key: key);
@override
Widget build(BuildContext context) {
return Container(
height: 100.0,
padding: EdgeInsets.all(8.0),
child: InkWell(
borderRadius: BorderRadius.circular(25.0),
splashColor: color,
onTap: () {
print('i am cool');
},
child: Row(
children: <Widget>[
Padding(
padding: EdgeInsets.all(16.0),
child: Icon(
iconLocation,
size: 60.0,
),
),
Text(
'Length',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 24.0,
),
),
],
),
),
);
}
}
Upvotes: 2
Views: 257
Reputation: 9569
There are several Dart-specific language features you don't see in C++, Python or Java. You can read more about all of them in the Dart documentation.
In Python, every argument is possibly nameable. In Dart, only the arguments wrapped in curly brackets can be referenced by their name in the constructor call. So putting curly brackets around arguments gives us this nice look on the caller side:
Category(
something: ...
somethingElse: ...
)
The initializer list is located between the constructor signature and the body. It runs before the body. In there, you can initialize instance variables, assert things and call super.
Why can't we just do these things in the constructor body, you might ask? Have a look at the next feature:
A constructor can be marked as constant if it fulfills several requirements (like that it has no constructor body and the class has only final fields). These additional requirements allow calls to the constructor to be marked as const
, causing the constructor to run at compile time rather than at runtime.
If you re-use a specific instance often (like, for example, EdgeInsets.all(16)
), all the instances will share the same memory location. So constructors marked with const
allow for the class to be directly embedded into the resulting program's memory at compile time.
Upvotes: 1
Reputation: 404
I have never coded dart. But this looks very similar to java constructors.
class Category extends StatelessWidget {
final String name;
final ColorSwatch color;
final IconData iconLocation;
const Category({
Key key,
@required this.name,
@required this.color,
@required this.iconLocation,
}) : assert(name != null),
assert(color != null),
assert(iconLocation != null),
super(key: key);
Constructors are usually used to initialise class properties (among other things). This one's taking three params, I'm assuming dart automatically assigns constructor params with matching names to corresponding class properties. It also has some validation rules like name != null etc.
Upvotes: 0