【发布时间】: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 方法实际上更改字段。但我对如何更新该嵌套字段感到困惑。
任何线索/帮助将不胜感激!
编辑:
- 我按照你说的做了并编辑了我的答案,但现在错误消失了,但似乎没有任何效果,即在 firestore 中,数据文档没有变化。
-
ERROR:Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>' in type cast
【问题讨论】:
标签: flutter dart google-cloud-firestore