如何在从另一个 Cupertino Picker 调用 onSelectedItemChanged() 时 Update/Refresh CupertinoPicker 列出数据
How to Update/Refresh CupertinoPicker List Data while onSelectedItemChanged() called from another Cupertino Picker
我在 BottomSheet 中有 2 个 CupertinoPicker,数据来自 Firestore,当我在省份更改所选项目时,它会更新另一个 cupertinopicker 中位置的列表数据
Here picture 你会在行中看到 4 个 Cupertino 选择器,但是第四个选择器不会更新,除非我关闭底部 sheet 并再次打开它所以如何 Update/Refresh CupertinoPicker 列表onSelectedItemChanged() 调用时的数据
here is some Code
Expanded(
child: StreamBuilder(
stream: _fireStore.collection('Locations').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Container();
}
return CupertinoPicker(
squeeze: 1.5,
diameterRatio: 1,
useMagnifier: true,
looping: true,
scrollController: _controllerPicker,
itemExtent: 33.0,
backgroundColor: Colors.white,
onSelectedItemChanged: (int index) => setState(() {
_pickerKey.currentState.build(context);
_getChosenGovLocation(snapshot
.data.documents[index].documentID);
}),
children: new List<Widget>.generate(
snapshot.data.documents.length, (int index) {
return new Center(
child: new Text(
'${snapshot.data.documents[index]['countryEN']}',
style: TextStyle(fontSize: 16),
),
);
}));
}),
),
Expanded(
child: CupertinoPicker.builder(
key: _pickerKey,
squeeze: 1.5,
diameterRatio: 1,
useMagnifier: true,
scrollController: new FixedExtentScrollController(
initialItem: 0,
),
itemExtent: 33.0,
backgroundColor: Colors.white,
onSelectedItemChanged: (int index) {
setState(() {
sortLocation = _sortBranches[index]['branchEN'];
});
print(sortLocation);
},
childCount: _sortBranches.length,
itemBuilder: (context, index) {
return new Center(
child: new Text(
'${_sortBranches[index]['branchEN']}',
style: TextStyle(fontSize: 16),
),
);
}),
),
and here is the Method that called while the selecte item changing
_getChosenGovLocation(id) {
_sortBranches.clear();
_fireStore.collection('Locations').document(id).snapshots().forEach((doc) {
setState(() {
_sortBranches = doc.data['branches'].toList();
print(_sortBranches.length);
});
});
print('list Called');
}
我已经解决了,刚刚使用 StatefullBuilder
它解决了问题
我在 BottomSheet 中有 2 个 CupertinoPicker,数据来自 Firestore,当我在省份更改所选项目时,它会更新另一个 cupertinopicker 中位置的列表数据
Here picture 你会在行中看到 4 个 Cupertino 选择器,但是第四个选择器不会更新,除非我关闭底部 sheet 并再次打开它所以如何 Update/Refresh CupertinoPicker 列表onSelectedItemChanged() 调用时的数据
here is some Code
Expanded(
child: StreamBuilder(
stream: _fireStore.collection('Locations').snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Container();
}
return CupertinoPicker(
squeeze: 1.5,
diameterRatio: 1,
useMagnifier: true,
looping: true,
scrollController: _controllerPicker,
itemExtent: 33.0,
backgroundColor: Colors.white,
onSelectedItemChanged: (int index) => setState(() {
_pickerKey.currentState.build(context);
_getChosenGovLocation(snapshot
.data.documents[index].documentID);
}),
children: new List<Widget>.generate(
snapshot.data.documents.length, (int index) {
return new Center(
child: new Text(
'${snapshot.data.documents[index]['countryEN']}',
style: TextStyle(fontSize: 16),
),
);
}));
}),
),
Expanded(
child: CupertinoPicker.builder(
key: _pickerKey,
squeeze: 1.5,
diameterRatio: 1,
useMagnifier: true,
scrollController: new FixedExtentScrollController(
initialItem: 0,
),
itemExtent: 33.0,
backgroundColor: Colors.white,
onSelectedItemChanged: (int index) {
setState(() {
sortLocation = _sortBranches[index]['branchEN'];
});
print(sortLocation);
},
childCount: _sortBranches.length,
itemBuilder: (context, index) {
return new Center(
child: new Text(
'${_sortBranches[index]['branchEN']}',
style: TextStyle(fontSize: 16),
),
);
}),
),
and here is the Method that called while the selecte item changing
_getChosenGovLocation(id) {
_sortBranches.clear();
_fireStore.collection('Locations').document(id).snapshots().forEach((doc) {
setState(() {
_sortBranches = doc.data['branches'].toList();
print(_sortBranches.length);
});
});
print('list Called');
}
我已经解决了,刚刚使用 StatefullBuilder
它解决了问题