1
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?

  • Does this answer your question? [what does super and Key does in flutter?](https://stackoverflow.com/questions/54968561/what-does-super-and-key-does-in-flutter) – Mariano Zorrilla May 09 '20 at 19:55
  • No it doesn't. I know both parts before and after the colon. But I don't understand why the colon itself is at the middle of the line. What is the purpose that it's serving? –  May 09 '20 at 19:59
  • Then you're looking for this: https://stackoverflow.com/a/50274735/3743245 – Mariano Zorrilla May 09 '20 at 20:03

1 Answers1

1

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;
}
TheMisir
  • 4,083
  • 1
  • 27
  • 37