flutter - Flutter,在 null 上调用了 getter 'currentState'
问题描述
我将数据从父级传递给子级,其中一个参数是 Fn。我需要的是再次将数据从孩子返回给父母,但我收到如下错误:
在 null 上调用了 getter 'currentState'。接收方:null 尝试调用:currentState
我在父窗口小部件中创建了一个函数,它执行一些操作,我设置父窗口的状态和 Fn 全局并将这个 Fn 传递给子窗口小部件,如下所示。
在这里,我将父状态设置为全局:
class ReservationBranchesSlotsScreen extends StatefulWidget {
**ReservationBranchesSlotsScreen({Key key}) : super(key: key);**
@override
ReservationBranchesSlotsScreenState createState() => ReservationBranchesSlotsScreenState();
}
class ReservationBranchesSlotsScreenState extends State<ReservationBranchesSlotsScreen> {
.
.
.
- 这是我从父母传递给孩子并将其设置为全局的 Fn
void changeData({
String areaId,
int coastPerPerson,
int selectedBranchChecked,
String formatted,
bool showCreateReservationButton,
bool isExpanded,
int expandedIndex
}){
setState(() {
_areaId = areaId;
_coastPerPerson = coastPerPerson;
_selectedBranchChecked = selectedBranchChecked;
_formatted = formatted;
_showCreateReservationButton = showCreateReservationButton;
_isExpanded = isExpanded;
_expandedIndex = expandedIndex;
});
}
- 在 build 方法中,我使用 State 中的键设置了 Scaffold 的键,还将 Fn(changeData) 传递给子小部件:
Widget build(BuildContext context) {
return Scaffold(
**key: widget.key,**
body: (_isCreateReservationButtonLoading)
? Center(
child: CircularProgressIndicator(backgroundColor: Colors.white),
)
: Container(
margin: EdgeInsets.only(bottom: 16),
child: SingleChildScrollView(
child: Column(children: <Widget>[
_isLoading
? Center()
: SelectBranchWidget(
parentKey: widget.key,
branches: _branches,
coastPerPerson: _coastPerPerson,
areaId: _areaId,
selectedBranchChecked: _selectedBranchChecked,
formatted: _formatted,
isExpanded: _isExpanded,
showCreateReservationButton: _showCreateReservationButton,
expandedIndex: _expandedIndex,
**changeData: changeData,**
),
- 下面是我需要从它向父级返回数据的子小部件:
class SelectBranchWidget extends StatefulWidget {
// here i create a Globalkey
**GlobalKey<ReservationBranchesSlotsScreenState> parentKey = GlobalKey<ReservationBranchesSlotsScreenState>();**
List<RestaurantBranch> branches;
int coastPerPerson;
String areaId;
int selectedBranchChecked;
String formatted;
bool isExpanded;
bool showCreateReservationButton;
int expandedIndex;
Function changeData;
SelectBranchWidget(
{
this.parentKey,
this.branches,
this.coastPerPerson,
this.areaId,
this.selectedBranchChecked,
this.formatted,
this.isExpanded,
this.showCreateReservationButton,
this.expandedIndex,
this.changeData,
});
@override
_SelectBranchWidgetState createState() => _SelectBranchWidgetState();
}
class _SelectBranchWidgetState extends State<SelectBranchWidget> {
void _changedValues(int i, int branchAreaIndex) {
widget.coastPerPerson = widget.branches[i].branchAreas[branchAreaIndex].costPerSeat;
widget.areaId = widget.branches[i].branchAreas[branchAreaIndex].guid;
print('areaId IS ${widget.areaId}');
widget.selectedBranchChecked = i;
widget.formatted ='';
widget.isExpanded = false;
widget.showCreateReservationButton = false;
widget.expandedIndex = -1;
// 这里我使用 currentState 来更新父级,数据发生了变化
**widget.parentKey.currentState.changeData(
areaId:widget.areaId,
coastPerPerson: widget.coastPerPerson,
selectedBranchChecked: widget.selectedBranchChecked,
formatted: widget.formatted,
showCreateReservationButton: widget.showCreateReservationButton,
isExpanded: widget.isExpanded,
expandedIndex: widget.expandedIndex,**
);
}
@override
Widget build(BuildContext context) {
return Container(
decoration: BoxDecoration(
color: Theme.of(context).primaryColorDark,
borderRadius: BorderRadius.all(Radius.circular(10))),
padding: EdgeInsets.all(8),
margin: EdgeInsets.only(left: 16, right: 16),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Align(
alignment: Alignment.centerLeft,
child: Text(
'Select Branch',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold),
),
),
SizedBox(
height: 8,
),
Align(
alignment: Alignment.centerLeft,
child: Padding(
padding: const EdgeInsets.only(left: 8.0),
child: Text(
'Number of branches = ${widget.branches.length}',
style: TextStyle(color: Colors.grey, fontSize: 10),
),
),
),
SizedBox(
height: 16,
),
ConstrainedBox(
constraints:
BoxConstraints(maxHeight: 230, maxWidth: double.infinity),
child: ListView.builder(
shrinkWrap: true,
itemCount: widget.branches.length,
itemBuilder: (ctx, i) {
return GestureDetector(
onTap: () {
if (widget.branches[i].branchAreas.length == 1) {
_changedValues(i, 0);
return;
}
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
backgroundColor: Theme.of(context).backgroundColor,
title: Text(
'Select Area',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold),
),
content: Container(
decoration: BoxDecoration(
color: Theme.of(context).primaryColorDark,
borderRadius: BorderRadius.all(
Radius.circular(10),
),
),
// height: (_branches[i].branchAreas.length == 1)
// ?70
// : (_branches[i].branchAreas.length == 2)
// ? 100
// :150,
width: 100.0,
child: ConstrainedBox(
constraints: BoxConstraints(maxHeight: 120),
child: ListView.builder(
shrinkWrap: true,
itemCount:
widget.branches[i].branchAreas.length,
itemBuilder:
(BuildContext context, int index) {
return GestureDetector(
onTap: () {
_changedValues(i, index);
Navigator.of(context).pop();
},
child: Padding(
padding: const EdgeInsets.only(
top: 16, left: 36.0, right: 36),
child: Column(
crossAxisAlignment:
CrossAxisAlignment.start,
children: [
Text(
widget.branches[i]
.branchAreas[index].name,
style: TextStyle(
color: Colors.white),
),
Divider(
color: Colors.grey,
thickness: 1,
),
],
),
),
);
},
),
),
),
);
});
},
child: Card(
.
.
.
解决方案
推荐阅读
- python - 如何在 matplotlib 中使用列名作为 x 轴值?
- php - 找不到 wkhtmltopdf 二进制文件
- javascript - 分发javascript算法
- python - 在 Python 中使用 selenium 和美丽的汤时在搜索框中键入文本
- java - Intellij 社区版:错误消息
- reactjs - 尝试发布新用户时反应 409 错误代码
- android - 如何使 Jetpack compose 元素出现在 android 的键盘顶部?
- javascript - 如何仅从左侧减小此 SVG 的宽度 其他应保持静态
- youtube - 我们可以在 YouTube 频道上上传网络系列并更改音频吗?是“OK”还是会有任何冒犯?
- php - 为什么我在 Laravel 5.4 中启用路由组时收到“NotFoundHttpException”?