- Initialize the Firestore instance: You first need to initialize the Firestore instance within your app. This can be done in the main.dart file through the following code:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(MyApp());
}
- Get a reference to the document: To add information to a pre-existing document, you need to first get a reference to that document. This can be done using the Firebase API through the following code:
final FirebaseFirestore _db = FirebaseFirestore.instance;
final DocumentReference documentReference = _db.collection('collectionName').doc('documentName');
- Create a Map of the data you want to add: You then need to create a Map of the data you want to add to the document. This can be done through the following code:
Map<String, dynamic> data = <String, dynamic>{
'field1': 'value1',
'field2': 'value2',
...
};
- Add the data to Firestore: Finally, you can add the data to Firestore through the following code:
await documentReference.update(data);
This will add the data to the pre-existing document.