dart – 将数据传递给StatefulWidget并在Flutter中以其状态访问它

我的Flutter应用程序中有2个屏幕:记录列表和创建和编辑记录的屏幕.

如果我将对象传递给第二个屏幕,这意味着我将编辑它,如果我传递null,则意味着我正在创建新项目.编辑屏幕是有状态的小部件,我不知道如何在我的情况下使用这种方法https://flutter.io/cookbook/navigation/passing-data/.

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) {
   //.....
  }
}

如何在_RecordPageState中访问recordObject?

要在_RecordPageState中使用recordObject,您必须像下面一样编写widget.objectname

class _RecordPageState extends State<RecordPage> {
  @override
  Widget build(BuildContext context) {
   .....
   widget.recordObject
   .....
  }
}
翻译自:https://stackoverflow.com/questions/50287995/passing-data-to-statefulwidget-and-accessing-it-in-its-state-in-flutter

转载注明原文:dart – 将数据传递给StatefulWidget并在Flutter中以其状态访问它