【问题标题】:Filter Firestore updating document过滤 Firestore 更新文档
【发布时间】:2021-03-15 16:31:13
【问题描述】:

当我尝试更新文档时,我的应用程序不断崩溃。我做错了什么?

 Future<bool> updateEvent(data, id) async {
    try {
      final _ref = _db.collection('content').doc(id);
      print(_ref);
      await _ref.update(data);

      return true;
    } catch (error) {
      print(error);
      return false;
    }
  }

print(_ref) 返回

flutter: DocumentReference(content/KwaKlb1GpUbqSQd8K9rd)

data是这样设置的

final data = {
      "active": true,
      "title": _titleTextController.text != null
          ? _titleTextController.text
          : null,
  ...
};

【问题讨论】:

  • 如果您可以打印错误并更新有问题的错误,那将非常有帮助
  • 我没有收到任何错误。这就是奇怪的地方。它只是说失去与设备的连接。
  • 进入logcat..在那里你可以找到错误。即使在应用崩溃后
  • 我不确定如何检查。但这会有所帮助。我将在线检查如何查看logcat。谢谢!我正在使用 iPhone 模拟器

标签: flutter google-cloud-firestore


【解决方案1】:
final data = {
  "active": true,
  if(_titleTextController.text != null)
  "title": _titleTextController.text
      
 };

只应在字段不为空时添加该字段,或者您可以使用 ??分配一个默认值

【讨论】:

  • 好的。有道理……但标题总是固定的。谢谢
【解决方案2】:

要正确回答,最好能看到您的错误消息。我目前没有将 Firestore 与 Flutter 一起使用,但如果它在其他平台上像 Firestore 一样工作,它将不允许将 Null 值设置为字段。当文本为空时,这将在您的示例中发生。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多