I'm trying to create a Flutter widget that can be initialized by various parameters, something like this
class MyWidget extends StatefulWidget {
final int? id;
final String? username;
MyWidget({this.id, this.username});
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
@override
void initState() {
super.initState();
if (widget.id != null) {
// init based on id
} else if (widget.username != null) {
// init based on username
} else {
// this should never happen
}
}
@override
Widget build(BuildContext context) {
return Container(); // build some widget
}
}
As you can see, neither of id
and username
are required, but I would need that at least one of them present. What would be a good way to approach this?