[英]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.您的
response
是DocumentSnapshot
,因此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.