AVAILABLE: 5
I have two screens in my Flutter app: a list of records and a screen for creating and editing records.
If I pass an object to the second screen, it means I am editing it, and if I pass null
, it means I am creating a new item. The editing screen is a Stateful widget, and I'm not sure how to use the approach described in this Flutter cookbook guide for my case.
class RecordPage extends StatefulWidget {
final Record recordObject;
RecordPage({Key key, @required this.recordObject}) : super(key: key);
@override
_RecordPageState createState() => new _RecordPageState();
}
class _RecordPageState extends State<RecordPage> {
@override
Widget build(BuildContext context) {
//.....
}
}
Question
How can I access recordObject
within _RecordPageState
?
Solution
To use recordObject
in _RecordPageState
, you simply reference it using widget.objectName
as shown below.
class _RecordPageState extends State<RecordPage> {
@override
Widget build(BuildContext context) {
.....
widget.recordObject
.....
}
}