gpt4 book ai didi

flutter 火炉 : updating a boolean field: type 'String' is not a subtype of type 'DocumentReference'

转载 作者:行者123 更新时间:2023-12-04 07:39:31 27 4
gpt4 key购买 nike

在我的 flutter 应用程序中,当单击复选框以指示已完成的任务时,理想情况下,我需要将其反射(reflect)在 Firestore 数据库中。有一个名为“todo”的集合,每个文档都有以下字段:task_id、user_id、done、task,其中 task 是实际要完成的事情(字符串),done 是一个 bool 字段。因此,当单击复选框时,我的代码会使用参数任务名称调用函数 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
}),
},
);
}
这个要更新的代码直接来自文档,显示的错误是 “String”类型不是“DocumentReference”类型的子类型 .
这两个打印语句也不起作用。我不知道为什么。
Error
这可能不需要,但这里是整个屏幕的代码仅供引用
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));
}
}

最佳答案

您似乎正在将文档 ID(它是 String)分配给 todoid这里

todoid = f.reference.id;
相反,您实际上需要分配一个 DocumentReference到它。所以你需要改变这个。
todoid = f.reference;
并且理想情况下也相应地更改变量的名称。

关于 flutter 火炉 : updating a boolean field: type 'String' is not a subtype of type 'DocumentReference' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67567221/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com