如何在 Flutter 中删除 DropdownMenuItem 中的重复项

问题描述 投票:0回答:1

我在从 Firestore 获取菜单项的值时遇到重复 DropdownMenuItem 的问题。

My Firestore DB Structure

我想从“routes”集合中获取所有“from”属性值(不包括重复项)。我想在我的 DropDownButton 中显示它们。

我的示例代码:

late final Stream<QuerySnapshot> routes = Database().getRoutes();


StreamBuilder<QuerySnapshot>(
stream: routes,
builder: (
    BuildContext context,
    AsyncSnapshot<QuerySnapshot> snapshot,
    ) {

  final data = snapshot.requireData;

  fromValue.value = data.docs[0]['from'];
  destinationValue.value = data.docs[0]['to'];

  return Column(
    children: [
      Obx(() {
        return DropdownButton(
          // Initial Value
          value: fromValue.value,
          // Down Arrow Icon
          icon: const Icon(Icons.keyboard_arrow_down),
          alignment: AlignmentDirectional.center,
          underline: Container(
            height: 1,
            color: Colors.deepPurple.shade100,
          ),
          // list of items
          items: data.docs.map(
                  (DocumentSnapshot doc) {
                return DropdownMenuItem<
                    String>(
                    value: doc['from']
                        .toString(),
                    child:
                    Text(doc['from']));
              }).toList(),
          // After selecting the desired option,it will
          // change button value to selected value
          onChanged: (newValue) {
            fromValue.value = newValue.toString();
          },
        );
      }),
    ],
  );
}),

用于获取 Firestore 数据的方法:

getRoutes() {
  final Stream<QuerySnapshot> routes = db
    .collectionGroup("routes")
    .snapshots();

    return routes;
  }
flutter firebase google-cloud-firestore dropdown stream-builder
1个回答
0
投票

我用过

factionDatabaseSnapshot.map((DocumentSnapshot document) => document['name']).toSet().toList().map((name)

使所有物品都独一无二。感谢Md。耶辛·谢赫

© www.soinside.com 2019 - 2024. All rights reserved.