简体   繁体   English

如何从 Firestore 集合中删除嵌套属性

[英]How to remove a nested property from Firestore collection

I am learning how to use "Firestore" with BLoC pattern in Flutter. I am following this tutorial by Sagar Suri.我正在学习如何在 Flutter 中使用 BLoC 模式的“Firestore”。我正在学习 Sagar Suri 的本教程。 https://medium.com/codechai/when-firebase-meets-bloc-pattern-fb5c405597e0 . https://medium.com/codechai/when-firebase-meets-bloc-pattern-fb5c405597e0 However, this tutorial is old and I am trying to remove bugs and update it for learning purpose.但是,本教程很旧,我正在尝试删除错误并更新它以供学习之用。 I am facing 2 issue in it.我面临其中的 2 个问题。 First issue is related with 'updateGoal' function. In example, he copied goals value from collection, cast it into the String and then updated the value.第一个问题与“updateGoal”function 有关。例如,他从集合中复制目标值,将其转换为字符串,然后更新值。 I am getting an error here.我在这里遇到错误。 Anybody can help me, how I can extract goals value from users, copy into Map, cast it and then update.任何人都可以帮助我,我如何从用户那里提取目标值,复制到 Map,投射然后更新。 在此处输入图像描述 . . This is what I am trying to do.这就是我想要做的。

Future<void> uploadGoal(String title, String documentId, String goal) async {
    DocumentSnapshot doc =
    await _firestore.collection("users").doc(documentId).get();
    Map<String, String> data = doc.data()! as Map<String, String>;
    /****/
    //Getting error here "The operator '[]' isn't defined for the type 'Object? Function()'."
    Map<String, String> goals = doc.data["goals"] != null
         ? doc.data["goals"].cast<String, String>()
         : null;
    /****/
    if (data != null) {
      data[title] = goal;
    } else {
      data = Map();
      data[title] = goal;
    }
    return _firestore
        .collection("users")
        .doc(documentId)
        .set({'goals': data, 'goalAdded': true},  SetOptions(merge: true));
  }

Similar issue, I am facing in removeGoal function.类似的问题,我在 removeGoal function 中面临。

void removeGoal(String title, String documentId) async {
    DocumentSnapshot doc =
    await _firestore.collection("users").doc(documentId).get();
    Map<String, String> data = doc.data()! as Map<String, String>;
    //How to remove goals title from collection here
    goals.remove(title);
    if (goals.isNotEmpty) {
      _firestore
          .collection("users")
          .doc(documentId)
          .update({"goals": goals});
    } else {
      _firestore
          .collection("users")
          .doc(documentId)
          .update({'goals': FieldValue.delete(), 'goalAdded': false});
    }
  }

Anybody can help me?任何人都可以帮助我吗? Thanks.谢谢。

This looks wrong:这看起来是错误的:

Map<String, String> data = doc.data()! as Map<String, String>;

While all the keys in your document are strings, the goals value is an object/dictionary instead of a string.虽然文档中的所有键都是字符串,但goals是对象/字典而不是字符串。 So at best you can cast it to:所以充其量你可以将它转换为:

Map<String, dynamic> data = doc.data()! as Map<String, dynamic>;

Once you do that, the statement you commented out to get the goals field should work, but it it doesn't: provide an updated in to your question with the updated code, and the exact error message and stack trace you get.执行此操作后,您为获取goals字段而注释掉的语句应该起作用,但它不起作用:使用更新的代码以及您获得的确切错误消息和堆栈跟踪为您的问题提供更新。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM