Flutter 从 firestore 数组填充下拉按钮

标签 flutter dart flutter-layout

如何将此数组列表填充到下拉按钮中?

我想将此位置列表填充到下拉按钮。 [这是我的数据库的样子] /image/S5yDh.png

最佳答案

我知道,现在回答你的问题已经太晚了。但也许,我的答案适合任何寻求答案的人。

只需使用 Streambuilder 在下拉列表中流式传输 Firestore 数据。 这是我的例子:

      StreamBuilder(
      stream: Firestore.instance.collection('your collectionName').document('yourDocument').snapshots(),
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return textCustom('Loading', Colors.black87, 16, 'Montserrat');
        } else {
          final data = snapshot.data['data'];
          return Theme(
            data: Theme.of(context).copyWith(
              canvasColor: Color(0xFF221F1F),
            ),
            child: DropdownButtonFormField(
              style: TextStyle(color: Colors.white70),                                               
              value: _value,
              items: data
                  .map<DropdownMenuItem<String>>(
                    (x) => DropdownMenuItem(
                      child: textCustom(
                          x, Colors.white, 16, 'Montserrat'),
                      value: '${x}',
                    ),
                  )
                  .toList(),
             
              onChanged: (val) => setState(() {
                _value = val;
           
              }),
            ),
          );
        }
      },
    )

关于Flutter 从 firestore 数组填充下拉按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61889520/

相关文章:

flutter - 在 flutter 中将字符串读取为 dart 代码的方法?

google-chrome - 当桌面网站打开的移动设备上的 Chrome 应用程序时,Flutter Web 无法正确呈现

firebase - 任务 ':app:transformClassesAndResourcesWithR8ForRelease' 执行失败。在尝试获取 flutter 应用程序的 apk 时

flutter - 保持 Flutter 的样式独立于 View 代码

flutter - 尝试显示字符串时,DropdownButtonFormField给出错误

listview - 为什么不推荐使用ExpansionTile页面?

dart - 如何使用 Dart :html library to write files?

flutter - 找不到用于flutter 1.12.13 + hotfix.5的名为 “pubspec.yaml”的文件

flutter - 图像小部件 : the height does not match the parent

flutter - 如何使用 GestureDetector 在卡片上添加飞溅效果