首页 > 解决方案 > Flutter & Firebase:有没有办法可以将特定字段从 firebase 返回到函数?

问题描述

用户>用户ID然后:

我的目标是从文档中返回用户的密钥,然后能够在其他功能中使用该密钥。

  getUsersKey() async {
   final uid = await getCurrentUser();
   Firestore.instance.collection('users').document(uid).get();
   // Then I want to return the userKey feild 
  }

标签: firebasefluttergoogle-cloud-firestore

解决方案


您可以编写以下代码:

 Future<void> getUsersKey() async {
   final uid = await getCurrentUser();

 DocumentSnapshot snapshot = 
 await Firestore.instance.collection('users').document(uid).get();
  userKey = snapshot.data['userKey'] //you can get any field value you want by writing the exact fieldName in the data[fieldName]
  }

推荐阅读