我怎么才能得到这个单一的数据“密钥”没有使用strembuilder或futurebuilder...In flutter使用FireBase我只想检查一下我的主密码==密钥有任何帮助吗?

发布于 2021-06-23 17:27:02
final result = await FirebaseFirestore.instance.doc('admin/6Ctki5kFhlcycvim7Mar').get();
final hashResult = result.data() as Map<String,dynamic>;
final password = hashResult['key'];您可以使用上面的代码片段获取密码值。
发布于 2021-06-25 00:31:56
具体步骤如下:
希望在执行以下步骤之前已经调用了Firebase.initializeApp();。下面的所有步骤都可以在单独的class或WidetNameState extends State<WidgetName>{}类中编写,您可以在该类中使用firestore数据。
第1步:创建Firestore实例。
FirebaseFirestore _firestore = FirebaseFirestore.instance;
第2步:创建一个从集合中以列表形式获取所有文档的函数:
///This function Returns the Collection Documents as a List.
///
/// Parameter [collection] takes the name of the collection to be queried.
Future<List<QueryDocumentSnapshot>> getCollectionDocumentsAsList(String collection) async {
final QuerySnapshot data = await _firestore.collection('$collection').get();
return data.docs;
}第4步:检查每个文档中的“密钥”是否与您的“主密码”匹配:
/// pass your master password to [masterPassword]
void checkPassword(String masterPassword)async{
/// this variable will contain the document from firestore that has the same key as your master password.
DocumentSnapshot passwordMatchDoc;
// admin is the collection name.
List<QueryDocumentSnapshot> documents = await getCollectionDocumentsAsList('admin');
documents.forEach((DocumentSnapshot doc){
if (doc["key"] == masterPassword){
passwordMatchDoc = doc;
}
});
}在这里,当密码匹配时,您还可以使用for循环,而不是.forEach((d){})和break。或者在函数中声明变量,然后将具有与主密码相同的密码的文档存储在其中。
您可以在小部件类的initState(){}函数中调用checkPassword('masterPassword')。
小提示,因为firestore集合中的文档id看起来像是自动生成的,所以在应用程序执行时无法知道文档的实际id,因此您必须遍历所有文档,并使用密码检查每个文档的key字段。但是将来,如果你知道你想要的特定键的文档id,你可以使用下面的函数,它以DocumentSnapshot的形式返回文档中的所有字段,这将减少你的计算时间。
/// Returns a [DocumentSnapshot] of [document] from [collection] specified.
Future<DocumentSnapshot> getCollectionDocument(String collection, String document) async => await _firestore.collection(collection).doc(document).get();https://stackoverflow.com/questions/68096803
复制相似问题