我刚刚开始研究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
我试图将其放在弯曲的括号和括号中,但似乎这里存在更深的问题。感谢您为理解问题提供的所有帮助!
答案 0 :(得分:3)
我对Firestore并不了解很多,但是查看API docs时,您需要使用SetOption
类进行合并:
Future setTodo(String key, bool value) async {
return await userToDos.doc(userID).set({key: value}, SetOptions(merge: true));
}