【问题标题】:FLUTTER FIRESTORE: updating a boolean field: type 'String' is not a subtype of type 'DocumentReference'FLUTTER FIRESTORE:更新布尔字段:“String”类型不是“DocumentReference”类型的子类型
【发布时间】:2021-08-06 13:40:35
【问题描述】:

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

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
          }),
        },
      );
}

此要更新的代码直接来自文档,显示的错误是 type 'String' 不是 type '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));
  }
}

【问题讨论】:

    标签: flutter google-cloud-firestore error-handling transactions


    【解决方案1】:

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

    todoid = f.reference.id;
    

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

    todoid = f.reference;
    

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

    【讨论】:

    • 谢谢你..这工作。你今天帮了我两次:)
    • 很高兴我能帮上忙 :)
    【解决方案2】:

    错误来自这一行

     DocumentSnapshot snapshot = await transaction.get(todoid);
    

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

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

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

    【讨论】:

      猜你喜欢
      • 2020-07-26
      • 1970-01-01
      • 2023-01-26
      • 2021-10-06
      • 1970-01-01
      • 2020-08-01
      • 2020-10-19
      • 2021-07-02
      • 2019-10-15
      相关资源
      最近更新 更多