简体   繁体   English

如何获取从 Dart Flutter Firebase 检索到的数据的值?

[英]How to get value of data retrieved from Dart Flutter Firebase?

I have a code like this:我有这样的代码:

  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  FirebaseFirestore FS = FirebaseFirestore.instance;

  CollectionReference TC = FirebaseFirestore.instance.collection("oneSignal");
  var docs = TC.doc("xcG9kfCWnUK7QKbK37qd");
  var response = await docs.get();
  
  var veriable = response.data();
  print(veriable);

I'm getting an output like this:我收到这样的 output:

{id: 5138523951385235825832}

I only want the value from this output, namely 5138523951385235825832 .我只想要这个 output 的值,即5138523951385235825832 How can I do that?我怎样才能做到这一点?

Your response is a DocumentSnapshot , so response.data() returns a Map with all the fields.您的responseDocumentSnapshot ,因此response.data()返回包含所有字段的Map To get a specific field from it you can do:要从中获取特定字段,您可以执行以下操作:

var veriable = response.data() as Map;
print(veriable["id"]);

Alternatively you can get the field directly from the response with:或者,您可以直接从响应中获取字段:

print(response.get("id"));

For situations like that I recommend keeping the documentation handy, in this case for DocumentSnapshot .对于这种情况,我建议将文档放在手边,在本例中为DocumentSnapshot

response.data() returns a Map. A Map is a key-value pair. response.data()返回一个 Map。Map 是一个键值对。 In Dart value of a Map can be accessed using a key.在 Dart 中,可以使用密钥访问 Map 的值。

void main() { 
   var details = {'Usrname':'tom','Password':'pass@123'}; 
   details['Uid'] = 'U1oo1'; 
   print(details); 
} 

It will produce the following output −它将产生以下 output -

{Usrname: tom, Password: pass@123, Uid: U1oo1}

In your case, you can get the value of id using veriable['id']在您的情况下,您可以使用veriable['id']获取 id 的值

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

相关问题 如何使用 flutter 中的子值从 firebase 获取数据? - How to get data from firebase using child value in flutter? 如何使用 Flutter/Dart 从 Firebase 实时数据库中读取数据 - How to read data from Firebase Realtime database using Flutter/Dart 如何从 firebase 获取数据并将其存储到列表中以供在 dart 中进一步操作 - How to get data from the firebase and store it into a list for futher manipulation in dart 如何获取从 Flutter 中的 Firebase Firestore 检索到的文档列表中存在的特定文档的索引? - How to get the index of a specific document present in the list of documents retrieved from Firebase Firestore in Flutter? 如何从flutter中获取firebase的数据 - how to get data from firebase in flutter 怎么才能等到Firebase Flutter 取回数据库数据呢? - How can I wait until Firebase Database data is retrieved in Flutter? 无法从 Dart Flutter FireBase 中提取数据 - Can't pull data from Dart Flutter FireBase 如何从 Firebase 实时数据库中获取数据到 Flutter 的列表中? - How to get data from Firebase Realtime Database into list in Flutter? 如何从 firebase 数据库 flutter 中的 map 获取值 - how to get value from map in firebase database flutter 如何在 Flutter 中用 boolean 值(真假)对来自 firebase 的数据进行排序? - How to sort data from firebase with boolean value (true and false) in Flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM