【问题标题】:Flutter: How to add many fields in firestore using one document without overwritingFlutter:如何在不覆盖的情况下使用一个文档在 Firestore 中添加多个字段
【发布时间】:2019-10-25 04:00:08
【问题描述】:

我的项目现在有问题。我知道,很多人已经发布了这个问题,但我真的不知道如何在我的代码中实现它。我想做的是,使用一个文档在 Firestore 字段中添加多个数据。在字段中,有一个字段名称“Issue”,在“Issue”里面我有很多数据。每次我在该文档中添加新数据时,它都会覆盖该字段中的数据。如何在不覆盖的情况下添加数据,请帮助。

这是我的代码:

_saveIssueToActivities(dynamic data) async {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
final FirebaseUser user = await _firebaseAuth.currentUser();
try {
  DocumentReference ref = db.collection('ACTIVITIES').document(user.uid);
  return ref.setData({
  'Issue': {
    'User_ID': '',
    'Name_ofUser': '${data['Name_ofUser']}',
    'Help_Description': '${data['Help_Description']}',
    'Help_DatePosted:': '',
    'Help_Location': '',
    'Help_TypeNeeded': '${data['Help_TypeNeeded']}',
    'Help_NotificationID': '',
  }
  });
} catch (e) {
  print(e);
}

}

这是我的数据库结构:

Link to my db pic

【问题讨论】:

  • 你尝试过 setData 方法中的 merge: true 可选参数吗?
  • 是的,马克先生,但它会覆盖旧数据,它不会添加新的“问题”:(

标签: flutter dart


【解决方案1】:

尝试:

Future<dynamic> addDataToFirestore(dynamic data) async {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
final FirebaseUser user = await _firebaseAuth.currentUser();
DocumentReference ref = db.collection('ACTIVITIES').document(user.uid);
dynamic datatoSubmit = [{
    'User_ID': '',
    'Name_ofUser': '${data['Name_ofUser']}',
    'Help_Description': '${data['Help_Description']}',
    'Help_DatePosted:': '',
    'Help_Location': '',
    'Help_TypeNeeded': '${data['Help_TypeNeeded']}',
    'Help_NotificationID': ''
}];
   //Do some debugging with the datatypes or which type of data you have in the firebase document.

await ref.updateData({'Issue': FieldValue.arrayUnion(datatoSubmit)});

}


Or


Future<dynamic> addDataToFirestore(dynamic data) async {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
final FirebaseUser user = await _firebaseAuth.currentUser();
DocumentReference ref = db.collection('ACTIVITIES').document(user.uid);
dynamic datatoSubmit = {
    'User_ID': '',
    'Name_ofUser': '${data['Name_ofUser']}',
    'Help_Description': '${data['Help_Description']}',
    'Help_DatePosted:': '',
    'Help_Location': '',
    'Help_TypeNeeded': '${data['Help_TypeNeeded']}',
    'Help_NotificationID': ''
};
   //Do some debugging with the datatypes or which type of data you have in the firebase document.

await ref.updateData({'Issue': FieldValue.arrayUnion(datatoSubmit)});

}

这是我第二个例子的结果:

【讨论】:

  • 将您的 Firebase 问题字段数据类型更改为数组。所以每次推送时都会添加一个新的数据条目。或者你能把输出错误贴在这里吗?
  • 还是一样,先生,这是消息:I/flutter (11998): type '_InternalLinkedHashMap' is not a subtype of type 'List'
  • 将您发送到 firebase 的数据包装到一个列表中。我已经更新了代码。正如我在评论中提到的调试数据类型。问题在于您在 firebase 中使用的数据类型和您要更新到 firebase 的数据都不同。这段代码对我来说很好用。
  • 是的,先生,它有效,但问题是如果我有一个空文档“updateData”将无法工作@@首先我需要使用“setData”然后“updataData”这样它会添加一个新的@@如果文档为空,我将如何设置条件使用 setData 但如果不是 updateData @@请帮忙。
猜你喜欢
  • 2018-08-29
  • 2014-08-10
  • 1970-01-01
  • 1970-01-01
  • 2022-11-02
  • 2015-10-11
  • 2022-01-26
  • 1970-01-01
  • 2021-11-27
相关资源
最近更新 更多