繁体   English   中英

如何使用 Dart 访问数据库中的值?

[英]How to access a value in the database using Dart?

我将 firestore 数据库中的文档放入名为“product”的键中,如下所示: {"wallname":"","height":0,"width":0,"wallcondition":0,"**product"**:null,"color":null,"area":0,"hoursrequired":0,"laborprice":0}

在此处输入图像描述

我的密钥中已经包含了整个数据库文档,

现在我想访问称为 image_product 的值(数据库中的值);
稍后我也想访问子集合。 任何人都可以帮助我吗? 非常感谢!

var document = data[索引]["产品"]; //这里来自数据库的文档 var image = document["image_product"]; //在这里我应该可以访问它但我无法返回图像;

如果您想从 firestore 中的文档访问值,您可以这样做:

final docSnapshot = await firestore.collection('you collection name')
    .doc('your document id').get();  
Map<String, dynamic> data = docSnapshot.data()!;

final fieldToAccess = data['fieldName'];
return fieldToAccess;

如果你想访问一个子集合,你应该重复这段代码,但像这样添加子集合:

final docSnapshot = await firestore.collection('your collection name')
    .doc('your doc id').collection('your subcollection name').doc('your other doc id').get();

在 firestore 中,当您访问集合中的文档时,您不会访问它们的子集合。 所以如果你想从子集合中获取数据你必须做另一个操作。

暂无
暂无

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

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