In the following code example, from the flutter docs:
class RandomWords extends StatefulWidget {
@override
createState() => RandomWordsState();
}
class RandomWordsState extends State<RandomWords> {
@override
Widget build(BuildContext context) {
final wordPair = WordPair.random();
return Text(wordPair.asPascalCase);
}
}
What exactly does the State<RandomWords>
syntax mean?
I understand that you can specify the type for the objects contained in a collection, like lists, using this syntax - List <String>
But I cannot understand the motive behind State<RandomWords>
.
Moreover, how can you reference RandomWordsState
in RandomWords
declaration and also reference RandomWords
in RandomWordsState
declaration? Shouldn't that cause a circular reference error or something?
I come from dynamically typed languages like python, and this looks a little odd to me, can someone please point me to the right place?
Dart objects have runtimeType property which returns Type . To check whether the object has a certain type, use == operator. Unlike is , it will only return true if compared to an exectly same type, which means comparing it with its super class will return false . void main() {
The syntax is a basic dart program that consists of various elements such as a keyword, an identifier, a constant, a string literal, data types, and symbols. Eg: to represent numbers words and even decimals we can call the type of data. Each line is dart must end with the semicolon.
There is actually no difference between using Object and dynamic in the example you have given here. There is no practical difference, and you can swap the two and the program will run the same. When I refer to "semantic difference", I mean how the code will be understood by other Dart programmers.
The object itself is what holds any specific data and logic. For example, a Cat class might look like this: class Cat { String name; String color; } The variable declarations above, String name and String color , are called properties or class members.
<RandomWords>
is a generic type parameter passed to the State
class.
The State
class looks like
abstract class State<T extends StatefulWidget> extends Diagnosticable {
and RandomWords
will be passed to the T
type parameter which has a constraint that T
needs to be a subclass of StatefulWidget
.
State
also has a field and getter where the type parameter is used
T get widget => _widget;
T _widget;
This results in a property of the type of the widget
which provides proper autocompletion and type checks in its subclass RandomWordsState
Assume you have
class RandomWords extends StatefulWidget {
RandomWords({this.fixed});
final WordPair fixed;
@override
createState() => RandomWordsState();
}
class RandomWordsState extends State<RandomWords> {
@override
Widget build(BuildContext context) {
// vvvv here we can access `fixed` in a strongly typed manner
final wordPair = widget.fixed ?? WordPair.random();
return Text(wordPair.asPascalCase);
}
}
See also https://www.dartlang.org/guides/language/language-tour#generics
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