更新 Firestore 中数组的映射数据 - Flutter

Fou*_*das 5 firebase flutter google-cloud-firestore flutter-provider

我一直在尝试创建一个由 Google Firebase 提供支持的演示“Schedule-App”,但在使用 Firestore 一侧的映射值更新数组的特定值时遇到一些困难。

如下图所示,我们想要实现的onButtonPress,要更新这个特定值。

在此输入图像描述

我创建了一个数据流并通过StreamProvider.

StreamProvider<DocumentSnapshot>.value(
        value: FirebaseFirestore.instance.collection('schedule').doc(FirebaseAuth.instance.currentUser.uid).snapshots(),
Run Code Online (Sandbox Code Playgroud)

我使用以下方法来更新数组的数据,但它不起作用。

  Future<void> updateUserData(int index, String title, String text, int hour, bool check) async {
    DocumentReference snapshots = FirebaseFirestore.instance.collection('schedule').doc(FirebaseAuth.instance.currentUser.uid);
    snapshots.update({
      ['standard_schedule'][index]: {'title': title, 'text': text, 'hour': hour, 'check': check}
    });
  }
Run Code Online (Sandbox Code Playgroud)

我应该对上面的方法进行哪些更改,以便每次按下按钮时更新 bool 值?

=================================================

解决方案(2020 年 10 月 24 日):Firestore不提供更新索引处数组元素的方法。您必须读取整个文档,修改内存中的数组,然后将整个数组写回文档。

Fou*_*das 3

Firestore不提供更新索引处的数组元素的方法。您必须读取整个文档,修改内存中的数组,然后将整个数组写回文档