I am using a StatefulWidget whose constructor contains an Object, like
class X extends StatefulWidget {
X({this.o});
Object o;
...
}
As it sometimes happens, this object o
changes, but the widget X
does not update, so I had to add an ObjectKey in the constructor:
class X extends StatefulWidget {
X({this.o, this.key});
Object o;
ObjectKey key;
...
}
Now every time I call the constructor, I have to call X(o: o, key: ObjectKey(o));
. Is there a better way of doing this? It would be nice, if the constructor itself manages that, like
class X extends StatefulWidget {
X({this.o, this.key = ObjectKey(o)});
Object o;
ObjectKey key;
...
}
But as ObjectKey(o)
is not a constant expression, it cannot be used as a default parameter. Is there another way?
You can accomplish what you want by using this notation in your constructor:
class X extends StatefulWidget {
X({this.o}) : key = ObjectKey(o);
Object o;
ObjectKey key;
}
This is called the initializer list, which you can find more about here.