如何在Firebase中管理添加和更新数据 [英] How to manage add and update data in Firebase
本文介绍了如何在Firebase中管理添加和更新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Flutter.我想先添加数据.如果已注册电子邮件或uid,我想更新data01/02或添加data01/02.
I'm using Flutter. I would like to add data at first. If email or uid had been registered, I want to update data01/02 or add data01/02.
如何指定初始数据.
_firestore.collection('members').add({
'uid': loginUser.uid,
'email': loginUser.email,
'data01': {
'name': name,
'img_url': 'https://www.xxx.xxx/xxx.png',
'data02': {
'phone': phone,
}
}
请给我建议.
推荐答案
假设您将文档名称设置为loginUser.uid,则可以使用:
Assuming you set your document name to your loginUser.uid you can use:
String rawJson = '{
"email": loginUser.email,
"data01": {
"name": name,
"img_url": "https://www.xxx.xxx/xxx.png",
"data02": {
"phone": phone,
}
}';
Map<String, dynamic> updateData = jsonDecode(rawJson); // import 'dart:convert';
final userDocument = await Firestore.instance.document('members/$loginUser.uid');
final userDocumentGet = userDocument.get().then((value) {
if (value == null || !value.exists) {
//a document for the member can NOT be found
Firestore.instance.document('members/$loginUser.uid').setData(updateData);
} else {
//a document for the member is found
Firestore.instance.document('members/$loginUser.uid').updateData(updateData);
}
});
这篇关于如何在Firebase中管理添加和更新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文