class User {
String id;
final DateTime birthday;
User(
{this.id, required this.birthday,});
Map<String, dynamic> toJson() => {
'birthday': birthday,
'id' : id,
};
My User model.
final user = User(
birthday: DateTime.parse(_date.text),
Future createUser(User user) async {
final docUser = FirebaseFirestore.instance.collection('users').doc();
user.id = docUser.id;
final json = user.toJson();
await docUser.set(json);
}
Where I am trying to save to Firestore.
DateTime? pickedDate = await showDatePicker(
context: context,
initialDate: DateTime.now(),
firstDate: DateTime(1900),
lastDate: DateTime.now());
if (pickedDate != null) {
setState(() {
_date.text = DateFormat('dd-MM-yyyy').format(pickedDate);
});
}
The dateFormat form where I want the user's birthday.
When I press the save button to save it to the database,
FormatException: Invalid date format 09-08-2012
Gives a fault.
Firebase uses ISO8061
format to save dates. Let us say your b'day is 08-11-2004 so your code would be so
final date = DateTime(2004, 11, 8).toIso8601String();
Now you can upload the date variable into firebase as Date
format.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.