I guess that would be better to merge the two answer that you received.
@mmcdon20 is right, you don't have to pass any argument to the state constructor because, as @Mohamed stated, you can do that via the init_state using the widget argument or in the _OverviewPageState
's constructor.
I am just doing the fusion of the answers here to be more precise for the ones who are new to Flutter.
The final result should look like this:
class OverviewPage extends StatefulWidget {
final int id;
const OverviewPage({Key? key, required this.id}) : super(key: key);
@override
_OverviewPageState createState() => _OverviewPageState();
}
class _OverviewPageState extends State<OverviewPage>{
late int idCopy;
// the 'late' keyword is necessary because otherwise Flutter would expect
// an initialization of the value from a Constructor that accept 'int id'
// as a parameter which is what we are trying to avoid because it's not the
// correct path to follow when you initialize a State of StatefulWidget
@override
void initState() {
super.initState();
idCopy = widget.id;
//your code here
}
}
or, using the constructor
class _OverviewPageState extends State<OverviewPage>{
late int idCopy;
_OverviewPageState() {
idCopy = widget.id; // here it is :)
}
}
Hope it clarify doubts for the newcomers :)
Credits: @Mohamed_Hammane, @mmcdon20