【问题标题】:How to update nested field inside a document in Firestore Flutter如何在 Firestore Flutter 中更新文档内的嵌套字段
【发布时间】:2021-12-14 08:46:04
【问题描述】:

我正在使用 Flutter 和 Cloud Firestore,但我被困在需要更新我的集合 -> 文档中的嵌套字段的点上。下面是我的 Firestore 集合结构的屏幕截图。我知道如何进行基本的更新方法,例如FirebaseFirestore.instance.collection('collection_path').doc('data').update(...),但是,就我而言,文档结构有点复杂,所以在执行更新方法时我需要一些帮助。

在这里,我需要在strategies 数组下将字段status 更改为true/false。 我可以访问strategyName 字段,以便我可以在strategies 数组中获取该项目并更改它...

我尝试了什么:

ElevatedButton(
                        onPressed: () async {
                          // status=true
                           final value = await FirebaseFirestore.instance
                              .collection(widget.symbol + '_Live')
                              .doc("data")
                              .get();
                          final data = value.data();
                          final strategies =
                              data!['strategies'] as List<Map<String, dynamic>>;
                          final strategy = strategies.firstWhere(
                              (item) => item['strategyName'] == strategyName,
                              orElse: () => Map());
                          strategy['status'] = true;
                          await FirebaseFirestore.instance
                              .collection(widget.symbol + '_Live')
                              .doc("data")
                              .update(data);
                        },
                        child: Text("Start"),
                      ),

显然,这不起作用,因为我只是在本地更改它,即不使用 Firestore update 方法实际上更改字段。但我对如何更新该嵌套字段感到困惑。

任何线索/帮助将不胜感激!

编辑

  1. 我按照你说的做了并编辑了我的答案,但现在错误消失了,但似乎没有任何效果,即在 firestore 中,数据文档没有变化。
  2. ERROR: Unhandled Exception: type 'List&lt;dynamic&gt;' is not a subtype of type 'List&lt;Map&lt;String, dynamic&gt;&gt;' in type cast

【问题讨论】:

    标签: flutter dart google-cloud-firestore


    【解决方案1】:

    您需要在本地更新value地图,然后将其存储回来:

    final value = await FirebaseFirestore.instance
          .collection(symbol + '_Live')
          .doc("data")
          .get();
    
      final data = value.data();
    
      final strategies =
          data!['strategies'].map((item) => item as Map<String, dynamic>).toList();
    
      final strategy = strategies.firstWhere(
          (item) => item['strategyName'] == strategyName,
          orElse: () => Map());
    
      strategy['status'] = toggleValue;
    
      await FirebaseFirestore.instance
          .collection(symbol + '_Live')
          .doc("data")
          .update(data);
    

    重点是存储整个value

    作者编辑:只需将每个元素映射到Map&lt;String, dynamic&gt;

    【讨论】:

    • 您好,谢谢您的回答,当我将.update(value) 行更改为.update(value as Map&lt;String, Object?&gt;) 后,它在启动时抛出以下错误:Unhandled Exception: type '_JsonDocumentSnapshot' is not a subtype of type 'Map&lt;String, Object?&gt;' in type cast
    • 您需要使用value.data() 而不是普通的value。请查看更新后的答案
    • 是的,请再次检查问题。
    • 我没有时间为你写代码,但你应该做的是遍历data['strategies']并将每个元素映射到Map&lt;String, dynamic&gt;。然后你就可以更新status
    • 感谢您抽出宝贵时间,我设法使它工作:D 谢谢先生!更改了您的答案。
    猜你喜欢
    • 1970-01-01
    • 2018-08-15
    • 2020-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多