Flutter:从Statefulwidget访问在_Widgetstatefulstate中声明和定义的变量

dgiusagp  于 2023-11-21  发布在  Flutter
关注(0)|答案(2)|浏览(174)

我需要访问一个在我的有状态小部件的状态中声明和定义的变量:

class StatItemDeclarable extends StatefulWidget {
  const StatItemDeclarable({Key? key, required this.title, required this.index}) : super(key: key);

  final String title;
  final int index;
  
  //Need to access variables here

  @override
  State<StatItemDeclarable> createState() => _StatItemDeclarableState(title: title, index: index);
}

class _StatItemDeclarableState extends State<StatItemDeclarable> {

  _StatItemDeclarableState({required this.title, required this.index});

  String title; // Variable to access
  final _titleController = TextEditingController();
  final int index;
  bool deleted = false;
  late Color _color; // Variable to access

字符串
我是一个新手,所以我会很感激一些帮助:)
我想要访问的变量在StatefulWidget中不可访问

qzwqbdag

qzwqbdag1#

你可以通过使用widget来访问这些变量。像这样:

widget.title;

字符串

rmbxnbpk

rmbxnbpk2#

你可以通过在vat名称前使用关键字widget来访问任何var声明的inn构造函数,如

widget.title;
widget.index;

字符串

相关问题