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

问题描述 投票:0回答:1

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

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

我对 flutter 还很陌生,所以我希望得到一些帮助:)

我想要访问的变量在 StatefulWidget 中无法访问

flutter variables statefulwidget
1个回答
0
投票

您只需使用小部件即可访问这些变量。像这样:

widget.title;
© www.soinside.com 2019 - 2024. All rights reserved.