FLUTTER FIRESTORE:更新布尔字段:类型 'String' 不是类型 'DocumentReference' 的子类型

FLUTTER FIRESTORE: updating a boolean field: type 'String' is not a subtype of type 'DocumentReference'

在我的 flutter 应用程序中,当单击复选框以指示已完成的任务时,理想情况下,我需要将其反映在 firestore 数据库中。有一个名为 'todo' 的集合,每个文档都有以下字段:task_id、user_id、done、task,其中 task 是实际要完成的事情(字符串),done 是一个布尔值场地。因此,当单击复选框时,我的代码调用带有参数任务名称的函数 invertDone。这基本上是使用任务名称来查询集合以找到正确的文档并获取 docId。然后获取快照并反转该文档的 'done' 字段。

Future<void> invertDone(String task) async {
  //get docid of task with task name..invert its bool field
  todo.where('task', isEqualTo: task).get().then(
        (QuerySnapshot snapshot) => {
          snapshot.docs.forEach((f) {
            todoid = f.reference.id;
            print('todoid found is $todoid');
            //new
            FirebaseFirestore.instance
                .runTransaction((transaction) async {
                  // Get the document
                  DocumentSnapshot snapshot = await transaction.get(todoid);
                  if (!snapshot.exists) {
                    throw Exception("User does not exist!");
                  }
                  temp = snapshot.data()['done'];
                  print('it was $temp');
                  temp = !temp;
                  print('now its $temp');
                  transaction.update(todoid, {'done': temp});

                
                })
                .then((value) => print("data updated"))
                .catchError((error) => print("Failed to update : $error"));
            //new done
          }),
        },
      );
}

此要更新的代码直接来自文档,显示的错误是 类型 'String' 不是类型 'DocumentReference' 的子类型。

另外两个打印语句也不起作用。我不知道为什么。

这可能不需要,但这里是整个屏幕的代码,仅供参考

bool temp;
var todoid;
int number = 0;
final auth = FirebaseAuth.instance;
Stream collectionStream =
    FirebaseFirestore.instance.collection('todo').snapshots();
CollectionReference main = FirebaseFirestore.instance.collection('maindb');
CollectionReference todo = FirebaseFirestore.instance.collection('todo');
final myController = TextEditingController();

class Todo extends StatefulWidget {
  final String docid;
  final bool isCaretaker;
  Todo({this.docid, this.isCaretaker});
  @override
  _TodoState createState() => _TodoState();
  static const String id = 'todoscreen';
}

Future<void> addTask(String task) async {
  Map<String, dynamic> data = {
    'task_id': number,
    'user_id': docid,
    'task': task,
    'done': false
  };
  await todo.add(data);
  number++;
}

Future<void> invertDone(String task) async {
  //get docid of task with task name..invert its bool field
  todo.where('task', isEqualTo: task).get().then(
        (QuerySnapshot snapshot) => {
          snapshot.docs.forEach((f) {
            todoid = f.reference.id;
            print('todoid found is $todoid');
            //new
            FirebaseFirestore.instance
                .runTransaction((transaction) async {
                  // Get the document
                  DocumentSnapshot snapshot = await transaction.get(todoid);
                  if (!snapshot.exists) {
                    throw Exception("User does not exist!");
                  }
                  temp = snapshot.data()['done'];
                  print('it was $temp');
                  temp = !temp;
                  print('now its $temp');
                  transaction.update(todoid, {'done': temp});

                  // Return the new count
                  //return temp;
                })
                .then((value) => print("data updated"))
                .catchError((error) => print("Failed to update : $error"));
            //new done
          }),
        },
      );
}

_openPopup(context) {
  Alert(
      context: context,
      title: "Add Todo",
      style: AlertStyle(titleStyle: Theme.of(context).textTheme.headline1),
      content: Column(children: <Widget>[
        TextField(
          controller: myController,
          decoration: InputDecoration(
            icon: Icon(Icons.check),
            labelText: 'Task',
            labelStyle: Theme.of(context).textTheme.headline1,
          ),
        ),
      ]),
      buttons: [
        DialogButton(
          color: Colors.transparent,
          onPressed: () {
            addTask(myController.text);
            Navigator.pop(context);
            myController.clear();
            //print(myController.text);
          },
          child: Text(
            "ADD TASK",
            style: Theme.of(context).textTheme.headline1,
          ),
        )
      ]).show();
}

class _TodoState extends State<Todo> {
  @override
  void dispose() {
    // Clean up the controller when the widget is disposed.
    myController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Color(0xFF602247),
          toolbarHeight: 50.0,
          centerTitle: true,
          title: Text(
            'VITALITY',
            style: Theme.of(context).textTheme.headline3,
          ),
          automaticallyImplyLeading: false,
          actions: [
            Visibility(
                visible: isCaretaker,
                child: IconButton(
                    icon: Icon(Icons.add, color: Colors.white, size: 30.0),
                    onPressed: () {
                      _openPopup(context);
                    })),
          ],
        ),
        body: StreamBuilder(
          stream: FirebaseFirestore.instance
              .collection('todo')
              .where('user_id', isEqualTo: docid)
              .snapshots(),
          builder: (context, snapshot) {
            return Center(
              child: ListView.builder(
                  itemCount: snapshot.data.docs.length,
                  itemBuilder: (context, index) {
                    DocumentSnapshot task = snapshot.data.docs[index];
                    bool _checked = task['done'];
                    return StatefulBuilder(
                        builder: (BuildContext context, StateSetter setState) {
                      return CheckboxListTile(
                        title: Text(task['task']),
                        value: _checked,
                        onChanged: isCaretaker
                            ? (bool value) {
                                print('do u want to delete ');
                              }
                            : (bool value) {
                                print('initially checked is $_checked');
                                setState(() {
                                  _checked = value;
                                  print(_checked.toString());
                                });
                                invertDone(task['task']);
                              },
                        secondary: const Icon(Icons.hourglass_empty),
                      );
                    });
                    // return ListTile(title: Text(course['task']));
                  }),
            );
          },
        )
        // Center(
        //   child: Column(
        //     children: <Widget>[
        //       Text(widget.docid),
        //       Text(widget.isCaretaker.toString()),
        //     ],
        //   ),
        // ),
        ,
        bottomNavigationBar: bottomAppBar(id: widget.docid));
  }
}

错误来自这一行

 DocumentSnapshot snapshot = await transaction.get(todoid);

get 方法需要类型为“DocumentReference”的参数,而您传递的是“String

您可以使用以下代码解决此问题:

DocumentReference documentReference = FirebaseFirestore.instance.collection("todo").doc(todoid);
DocumentSnapshot snapshot = await transaction.get(documentReference);

您似乎在此处将文档 ID(String)分配给 todoid

todoid = f.reference.id;

当您实际上需要为其分配 DocumentReference 时。所以你需要改成这个。

todoid = f.reference;

最好相应地更改变量的名称。