CodeBlanks Logo Icon CodeBlanks

Add data

Add a document with collection().add.

Adding data

You add a new document to a collection with collection().add({...}). add generates an automatic document id and asynchronously returns a DocumentReference. The document data is given as a Map<String, dynamic>.

final ref = await db.collection('messages').add({
  'text': 'Merhaba',
  'sender': 'Ali',
});
print(ref.id);

doc().set with an explicit id

If you want to give your own id instead of an auto-generated one, you use doc('id').set({...}). This method overwrites the document if it exists and creates it if not. add is for new documents, while set is for writing with a specific id.

await db.collection('users').doc('u1').set({
  'name': 'Ali',
});

Practice Add a document and print its id

Get db. Add a document with data {'text': 'Merhaba'} to the 'messages' collection. Print the id of the returned reference.

Starter code

main.dart
class DocumentReference {
  final String id;
  DocumentReference(this.id);
}

class CollectionReference {
  final String name;
  final List<Map<String, dynamic>> _store;
  CollectionReference(this.name, this._store);

  Future<DocumentReference> add(Map<String, dynamic> data) async {
    final id = 'auto-${_store.length + 1}';
    _store.add(data);
    return DocumentReference(id);
  }
}

class FirebaseFirestore {
  static final FirebaseFirestore instance = FirebaseFirestore._();
  FirebaseFirestore._();
  final Map<String, List<Map<String, dynamic>>> _db = {};

  CollectionReference collection(String name) {
    _db.putIfAbsent(name, () => []);
    return CollectionReference(name, _db[name]!);
  }
}

void main() async {
  // belge ekle, id yazdır
}

Sample solution

main.dart
class DocumentReference {
  final String id;
  DocumentReference(this.id);
}

class CollectionReference {
  final String name;
  final List<Map<String, dynamic>> _store;
  CollectionReference(this.name, this._store);

  Future<DocumentReference> add(Map<String, dynamic> data) async {
    final id = 'auto-${_store.length + 1}';
    _store.add(data);
    return DocumentReference(id);
  }
}

class FirebaseFirestore {
  static final FirebaseFirestore instance = FirebaseFirestore._();
  FirebaseFirestore._();
  final Map<String, List<Map<String, dynamic>>> _db = {};

  CollectionReference collection(String name) {
    _db.putIfAbsent(name, () => []);
    return CollectionReference(name, _db[name]!);
  }
}

void main() async {
  final db = FirebaseFirestore.instance;
  final ref = await db.collection('messages').add({
    'text': 'Merhaba',
  });
  print(ref.id);
}

Expected output

auto-1

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does the collection().add method return?

  • An empty Future
  • Only an error code
  • An int number
  • DocumentReference Correct answer

Explanation: The add method returns a DocumentReference pointing to the new auto-id document.

2 Add a document

Question
final ref = await db.collection('messages').____({
  'text': 'Merhaba',
});
Answer
final ref = await db.collection('messages').add({
  'text': 'Merhaba',
});

Explanation: To add a new document to a collection you use the add method; the data is given as a Map.

3 Data writing parts

Term Meaning
add Auto-id document
doc('u1').set Specific-id document
Map<String, dynamic> Document data
DocumentReference Write result

All lessons in Advanced