如何从另一个Cupertino Picker调用onSelectedItemChanged()时更新/刷新CupertinoPicker列表数据

时间:2019-09-15 12:12:49

标签: flutter dart cupertinopicker

我在BottomSheet中有2个CupertinoPicker,当我更改省中的选定项目时,数据来自Firestore,它更新了另一个cupertinopicker中位置的列表数据

Here picture,您将在“行”中看到4 Cupertino选择器,但除非我关闭底部工作表并再次打开它,否则第四选择器不会更新,以便在调用onSelectedItemChanged()时如何更新/刷新CupertinoPicker列表数据

  

这是一些代码

 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),
                      ),
                    );
                  }),
            ),
  

这是在选择项更改时调用的方法

   _getChosenGovLocation(id) {
    _sortBranches.clear();

    _fireStore.collection('Locations').document(id).snapshots().forEach((doc) {
      setState(() {
        _sortBranches = doc.data['branches'].toList();

        print(_sortBranches.length);
      });
    });

    print('list Called');
  }

1 个答案:

答案 0 :(得分:0)

我已经使用StatefullBuilder修复了问题,