繁体   English   中英

如何获取子集合中文档的ID?

[英]How to get the ID of the document in a subcollection?

我想使用“食物”子集合中的数据进行 CRUD,但我不明白如何获取子集合的 ID 在此处输入图片说明

restaurantCollection
        .doc(uid)
        .collection('food')
        .doc(docId)
        .update(data);

我知道这是为了更新 food 子集合中的字段,但我如何实际获得docId

final CollectionReference restaurantCollection =
      FirebaseFirestore.instance.collection('restaurant');

Future addFoodData(String description, String name, double oriPrice,
      double salePrice, int pax) async {
    return await restaurantCollection.doc(uid).collection('food').doc().set({
      'ruid': uid, //i stored the parent's ID in the child
      'name': name,
      'description': description,
      'oriPrice': oriPrice,
      'salePrice': salePrice,
      'pax': pax,
    });
  }

 List<Food> _foodListFromSS(QuerySnapshot snapshot) {
    return snapshot.docs.map((doc) {
      return Food(
        name: doc.data()['name'] ?? '',
        description: doc.data()['description'] ?? '',
        oriPrice: doc.data()['oriPrice'] ?? 0.0,
        salePrice: doc.data()['salePrice'] ?? 0.0,
        pax: doc.data()['pax'] ?? 0,
      );
    }).toList();
  }

Stream<List<Food>> get food {
    return restaurantCollection
        .doc(uid)
        .collection('food')
        .snapshots()
        .map(_foodListFromSS);
  }

^database.dart 这是我创建数据的方式。

class Menu extends StatefulWidget {
  @override
  _MenuState createState() => _MenuState();
}

class _MenuState extends State<Menu> {
  bool loading = false;

  @override
  Widget build(BuildContext context) {
    final user = Provider.of<Users>(context);
    return loading
        ? Loading()
        : StreamProvider<List<Food>>.value(
            initialData: [],
            value: DatabaseService(uid: user.uid).food,
            child: Scaffold(
              appBar: ElrAppBar('Menu', false),
              drawer: ElrDrawer(),
              floatingActionButton: FloatingActionButton(
                onPressed: () {
                  print('ADD MENU Pressed');
                  Navigator.push(
                    context,
                    MaterialPageRoute(
                      builder: (context) => MenuAdd(),
                    ),
                  );
                },
                child: const Icon(Icons.add),
              ),
              body: Container(
                padding: EdgeInsets.symmetric(horizontal: 10),
                width: double.infinity,
                color: Colors.grey,
                child: FoodList(),
              ),
            ),
          );
  }
}

^菜单.dart

class FoodList extends StatefulWidget {
  @override
  _FoodListState createState() => _FoodListState();
}

class _FoodListState extends State<FoodList> {
  bool loading = false;

  @override
  Widget build(BuildContext context) {
    final food = Provider.of<List<Food>>(context);
    return loading
        ? Loading()
        : ListView.builder(
            itemCount: food.length,
            itemBuilder: (context, index) {
              return GestureDetector(
                onTap: () => Navigator.push(context,
                    MaterialPageRoute(builder: (context) => MenuUpdate())),
                child: Card(
                    margin: EdgeInsets.fromLTRB(0, 10, 0, 0),
                    child: FoodTile(food: food[index])),
              );
            },
          );
  }
}

^food_list.dart

class FoodTile extends StatelessWidget {
  final Food food;
  FoodTile({this.food});

  @override
  Widget build(BuildContext context) {
    return Card(
      child: Container(
        margin: EdgeInsets.all(5),
        child: Row(
          children: [
            Container(
              color: colorsConst[900],
              width: 100,
              height: 100,
            ),
            SizedBox(width: 10),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  food.name.inCaps,
                  style: TextStyle(
                    fontWeight: FontWeight.bold,
                    fontSize: 20,
                  ),
                ),                
                SizedBox(height: 4),
                Container(
                  width: 260,
                  child: Text(
                    food.description.inCaps,
                    style: TextStyle(fontSize: 15, fontStyle: FontStyle.italic),
                  ),
                ),
                SizedBox(height: 4),
                foodInfoCard('Original price: ',
                    'RM ${food.oriPrice.toStringAsFixed(2).toString()}'),
                SizedBox(height: 4),
                foodInfoCard('Selling price: ',
                    'RM ${food.salePrice.toStringAsFixed(2).toString()}'),
                SizedBox(height: 4),
                foodInfoCard('Available pax: ', food.pax.toString()),
              ],
            ),
          ],
        ),
      ),
    );
  }

^food_tile.dart

这是我从子集合中获取食物清单的方式。 当我单击 food_list.dart 中的卡片时,我不知道如何仅从子集合中返回一个特定数据。

我遇到了一个死胡同,我很迷茫。 我希望有一些愿意帮助我的人。 如果需要,请询问更多信息。 先感谢您!

您应该已将该id存储在您的应用程序中。 您已经拥有父类别的uid 而且food是不变的。 在更新之前如何获取原始数据? 如果没有 ID,则无法更新文档。 如果您要在food子集合中创建新文档,请使用.set()

当您执行此操作时:

Future addFoodData(String description, String name, double oriPrice,
      double salePrice, int pax) async {
    return await restaurantCollection.doc(uid).collection('food').doc().set({
      'ruid': uid, 
      'id': generateRandowId,//Create an ID and store it here.
      'name': name,
      'description': description,
      'oriPrice': oriPrice,
      'salePrice': salePrice,
      'pax': pax,
    });
  }

将来当您获取这些文档时,它会有一个“ID”字段,您可以使用它来更新它。 因为您在子集合中有文档的父ruidfood以及现在的id 我还建议您从这些文档中创建对象\\模型。 在处理['keys']时,这将使使用它们更容易,并且更不容易出错。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM