gpt4 book ai didi

firebase - 错误 : No named parameter with the name 'merge'

转载 作者:行者123 更新时间:2023-12-03 02:52:07 25 4
gpt4 key购买 nike

我刚开始研究 Flutter,并尝试将一个简单的 Todo 应用程序连接到 Firestore。为此,我想使用 setOption 'merge',这样之前的列表就不会被删除。但是,每次我尝试调试时,都会收到错误消息“没有名称为‘merge’的命名参数”。这是合并的代码:

import 'package:cloud_firestore/cloud_firestore.dart';

class DatabaseService {
final String userID;
DatabaseService(this.userID);

final CollectionReference userToDos =
FirebaseFirestore.instance.collection('userToDos');

Future setTodo(String key, bool value) async {
return await userToDos.doc(userID).set({key: value}, merge: true);
}

Future deleteTodo(String key) async {
return await userToDos.doc(userID).update({
key: FieldValue.delete(),
});
}

下面是剩余的代码:

import 'package:flutter/material.dart';
import 'add_item_dialog.dart';
import 'to_do_item.dart';
import 'database.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

void main() => runApp(MaterialApp(home: ToDo()));

class ToDo extends StatefulWidget {
@override
_ToDoState createState() => _ToDoState();
}

class _ToDoState extends State<ToDo> {
User user;
DatabaseService database;

void addItem(String key) {
database.setTodo(key, false);
Navigator.pop(context);
}

void deleteItem(String key) {
database.deleteTodo(key);
}

void toggleDone(String key, bool value) {
database.setTodo(key, !value);
}

void newEntry() {
showDialog<AlertDialog>(
context: context,
builder: (BuildContext context) {
return AddItemDialog(addItem);
});
}

Future<void> connectToFirebase() async {
final FirebaseAuth authenticate = FirebaseAuth.instance;
UserCredential result = await authenticate.signInAnonymously();

user = result.user;

// get [DatabaseService] instance for current user
database = DatabaseService(user.uid);

if (!(await database.checkIfUserExists())) {
database.setTodo('Todo anlegen', false);
}

Stream userDocumentStream = database.getTodos();
userDocumentStream
.listen((documentSnapshot) => print(documentSnapshot.data));
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('To-Do-App'),
backgroundColor: Color.fromRGBO(35, 152, 185, 100),
),
body: FutureBuilder(

// Wait until [connectToFirebase] returns stream
future: connectToFirebase(),
builder: (BuildContext context, AsyncSnapshot<void> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: CircularProgressIndicator());
} else {
// When stream exists, use Streambilder to wait for data
return StreamBuilder<DocumentSnapshot>(
stream: database.getTodos(),
builder: (BuildContext context,
AsyncSnapshot<DocumentSnapshot> snapshot) {
if (!snapshot.hasData) {
return Center(child: CircularProgressIndicator());
} else {
// resolve stream... Stream<DocumentSnapshot> -> DocumentSnapshot -> Map<String, bool>
Map<String, dynamic> items = snapshot.data.data;

return ListView.builder(
itemCount: items.length,
itemBuilder: (context, i) {
String key = items.keys.elementAt(i);
return ToDoItem(
key,
items[key],
() => deleteItem(key),
() => toggleDone(key, items[key]),
);
});
}
},
);
}
}),
floatingActionButton: FloatingActionButton(
onPressed: newEntry,
child: Icon(Icons.add),
backgroundColor: Color.fromRGBO(35, 152, 185, 100),
),
);
}
}

依赖关系:

  flutter:
sdk: flutter


# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.3

# Social sharing buttons
share: '>=0.6.5 <2.0.0'


# Google Firestore packages
cloud_firestore: ^0.14.0+2
firebase_auth: ^0.18.0+1

我试着把它放在圆括号和括号中,但似乎这里有更深层次的问题。我感谢所有帮助理解问题的人!

最佳答案

我对 firestore 了解不多,但正在查看 API docs您需要使用 SetOption 类进行合并:

 Future setTodo(String key, bool value) async {
return await userToDos.doc(userID).set({key: value}, SetOptions(merge: true));
}

关于firebase - 错误 : No named parameter with the name 'merge' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63720727/

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