add method
Adds a new document to the collection.
Creates a new document in Firestore and returns its generated ID.
The document is automatically serialized using the object's json getter.
Usage
final user = User(uid: '', name: 'John Doe', email: 'john@example.com');
final userId = await userCollection.add(object: user);
print('User added with ID: $userId');
Parameters
- object: The object to add (must implement InterfacePlus)
Returns
- String: The generated document ID
Error Handling
This method can throw Firestore exceptions. Always wrap in try-catch:
try {
final userId = await userCollection.add(object: user);
print('Success: $userId');
} catch (e) {
print('Error adding user: $e');
}
Implementation
Future<String> add({required InterfacePlus object}) async {
DocumentReference ref = _getCollection.doc();
await ref.set(object.json);
return ref.id;
}