Reputation:
class SharedPreferencesDemo extends StatefulWidget {
SharedPreferencesDemo({Key key}) : super(key: key); <-------- This line
@override
SharedPreferencesDemoState createState() => SharedPreferencesDemoState();
}
I can understand the part before and after the colon, but what does the colon at the middle mean? I am talking about the colon sitting in between those two parts of the line. What does the line ultimately mean?
Upvotes: 1
Views: 197
Reputation: 4279
This is called constructor. Used to create new instance of class by calling new SharedPreferencesDemo()
or just SharedPreferencesDemo()
.
SharedPreferencesDemo({Key key}) : super(key: key);
Here's explanation of each part.
SharedPreferencesDemo - constructor name
(...) - constructor arguments
{Key key} - optional named arguments
: - initializer list, used to call super or initialize variables including final ones
super - calls parent constructor (StatefulWidget.StatefulWidget)
key: key - sets value of optional argument [key] for parent constructor
Initializer list is used to initialize final variables or call constructor with specified arguments. Here's another example:
class AuthClient {
AuthClient({ String username, String password }) :
_token = '$username:$password';
final String _token;
}
Upvotes: 1