flutter - 如何从小部件中检索 value

我对编码很陌生,所以我不太了解,对不起。我试图让 value 从屏幕 1 传递到屏幕 2,但 value 位于 stateful 的小部件内。像那样:

class Escrita extends StatefulWidget {


  List<Diario> _diarioupdate = <Diario>[];
  Escrita({Diario? teste});



  @override
  State<Escrita> createState() => _EscritaState();
}

我想检索从屏幕 1 传递的 value 睾丸,我该怎么做?

回答1

我相信您在问如何访问 stateful 小部件中的 _diarioUpdate 。这可以通过从 _diarioupdate 中删除下划线 (_) 来完成,这样它就不会是私有成员,然后在 statefulwidget 中将其调用为 widget.diarioupdate

回答2

我找到了答案。非常感谢你的帮助。我这样解决:

Escrita({Diario? teste}){
this.teste};

回答3

_EscritaState 中你可以调用 widget._diarioupdate

例如你有 HomeWidget 作为 StatefulWidget 需要一个 title

class HomePage extends StatefulWidget {
  final String title;

  const HomePage({
    Key? key,
    required this.title,
  }) : super(key: key);

  @override
  State<HomePage> createState() => _HomePageState();
}

您可以使用 State 类中的 widget 字段访问 title value

class _HomePageState extends State<HomePage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title), // <----
      ),
      body: Container()
    );
  }
}

相似文章

随机推荐

最新文章