You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm slowly learning firestore and trying to create a new document that will have two fields as references to other documents. As a string I have no problem storing them, but it would be much more convenient to have them as references. The application when adding closes without any error.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm slowly learning firestore and trying to create a new document that will have two fields as references to other documents. As a string I have no problem storing them, but it would be much more convenient to have them as references. The application when adding closes without any error.
Platform: Windows
Future<void> add(Products product) async { try { await _firestoreService.addDocument( collectionPath: collection, data: product.toMap(), documentId: product.name, ); } catch (e) { throw Exception('Nie udało się dodać produktu'); } }
Products _buildProductData() { return Products( id: '', name: _nameController.text, ean: _eanController.text, brand: _selectedBrand!.reference!, category: _activeCategory.toList()[0].doc.reference, ); }
`import 'package:cloud_firestore/cloud_firestore.dart';
class Products {
final String id;
final String name;
final String ean;
final DocumentReference brand;
final DocumentReference category;
Products({
required this.id,
required this.name,
required this.ean,
required this.brand,
required this.category,
});
factory Products.fromFirestore(DocumentSnapshot doc) {
final data = doc.data() as Map<String, dynamic>;
return Products(
id: doc.id,
name: data['name'] ?? '',
ean: data['ean'] ?? '',
brand: data['brand'],
category: data['category'],
);
}
Map<String, dynamic> toMap() {
return {
'name': name,
'ean': ean,
'brand': brand,
'category': category,
};
}
}`
Beta Was this translation helpful? Give feedback.
All reactions