Reputation: 4392
I looked at the docs of dart for generics.
abstract class StringCache {
String getByKey(String key);
void setByKey(String key, String value);
}
abstract class ObjectCache {
Object getByKey(String key);
void setByKey(String key, Object value);
}
The above two is replaceed by one single generic type T with below code
abstract class Cache<T> {
T getByKey(String key);
void setByKey(String key, T value);
}
Where the use of T
is seen clearly. but not sure where the state class uses
class _CounterState extends State<Counter> {
int _counter = 0;
void _increment() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Row(
children: <Widget>[
RaisedButton(
onPressed: _increment,
child: Text('Increment'),
),
Text('Count: $_counter'),
],
);
}
}
Upvotes: 1
Views: 1295
Reputation: 658067
T
is a generic type parameter and extends StatefulWidget
is a constraint for what types T
can be set to.
With
class _CounterState extends State<Counter> {
T
is set to type Counter
(which has to be a StatefulWidget
).
Passing Counter
as type allows you to use
widget.foo
to reference field foo
in Counter
from _CounterState
and you get autocompletion and static type checking.
Upvotes: 3