I want to add the name of the user as a map containing 'firstName' and 'lastName'. I already have the user model and the name model, here is the code:
name_model.dart
class Name {
final String firstName;
final String lastName;
Name({
required this.firstName,
required this.lastName,
});
Map<String, dynamic> toMap() {
return {
'firstName': firstName,
'lastName': lastName,
};
}
Name.fromMap(Map<String, dynamic> nameMap)
: firstName = nameMap['firstName'],
lastName = nameMap['lastName'];
}
user_model.dart
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:ukk_backup/models/name_model.dart';
class User {
final String? docId;
final String email;
final String password;
final String phoneNumber;
final Name name;
User({
this.docId,
required this.email,
required this.password,
required this.phoneNumber,
required this.name,
});
Map<String, dynamic> toMap() => {
'docId': docId,
'email': email,
'password': password,
'phoneNumber': phoneNumber,
'name': name.toMap(),
};
User.fromDocumentSnapshot(DocumentSnapshot<Map<String, dynamic>> doc)
: docId = doc.id,
email = doc['email'],
password = doc['password'],
phoneNumber = doc['phoneNumber'],
name = Name.fromMap(doc['name']);
}
when I pressed the register button I want all the data to be saved in cloud firestore, including the name with the map type. This is the onPressed:
onPressed: (() async {
if (_formKey.currentState!.validate()) {
DatabaseService service = DatabaseService();
User user = User(
email: emailController.text,
password: passwordController.text,
phoneNumber: phoneNumberController.text,
name: , //what to type?
);
setState(() {
_isLoading = true;
});
await service.addUser(user);
setState(() {
_isLoading = false;
});
}
}),
If you found some of my code is not correct or you found the solution, please tell me. Thank you.
Edit:
database_services.dart
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:ukk_backup/models/user_model.dart';
class DatabaseService {
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
addUser(User userData) async {
await _firestore.collection('users').add(userData.toMap());
}
updateUser(User userData) async {
await _firestore
.collection('users')
.doc(userData.docId)
.update(userData.toMap());
}
Future<void> deleteUser(String documentId) async {
await _firestore.collection('users').doc(documentId).delete();
}
Future<List<User>> retrieveUser() async {
QuerySnapshot<Map<String, dynamic>> snapshot =
await _firestore.collection('users').get();
return snapshot.docs
.map((docSnapshot) => User.fromDocumentSnapshot(docSnapshot))
.toList();
}
}
CodePudding user response:
I misunderstood your question first. Your DataBaseService class looks ok to me. So if I am right the only problem you have is that you do not know how to add the name to the UserObject. If that's the essence of your question then that is very easy:
onPressed: (() async {
if (_formKey.currentState!.validate()) {
DatabaseService service = DatabaseService();
User user = User(
email: emailController.text,
password: passwordController.text,
phoneNumber: phoneNumberController.text,
name: Name(firstName: firstNameController.text, lastName: lastNameController.text),
);
setState(() {
_isLoading = true;
});
await service.addUser(user);
setState(() {
_isLoading = false;
});
}
}),