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

标签 flutter dart cupertinopicker

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

Here picture您将在 Row 中看到 4 个 Cupertino Picker,但第四个 Picker 不会更新,除非我关闭底部工作表并再次打开它,因此如何在 onSelectedItemChanged() 调用时更新/刷新 CupertinoPicker 列表数据

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它解决了问题

关于flutter - 如何在从另一个 Cupertino Picker 调用 onSelectedItemChanged() 时更新/刷新 CupertinoPicker 列表数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57943996/

相关文章:

flutter - Flutter 中的 IOS 日期选择器

dart - flutter 如何制作动态的多列 cupertinoPicker?

flutter - 为 Cupertino Picker 设置宽度和高度

flutter - 阻止动画立即播放

android - 当用户从 flutter 的设置屏幕导航回应用程序时检测方法

android - 如何重新加载 future 数据的屏幕

dart - Flutter - 键入文本动画

Flutter 使用 BloC 模式从 API 结果中获取列表元素

user-interface - 如何在 Flutter 中使用锚定按钮制作灵活的列表布局?

flutter - 在 Flutter 中将数据传递给有状态的小部件