I have 2 dropdown that return the String variable from the String of the list after the user chooses one of the list and a button that handle update data to firestore.
This is a variable I created
String? selectedMonthEnd;
String? selectedYearEnd;
This is the code of 2 dropdowns
final month = [
'January','Febuary','March','April','May','June','July','August','September','October','November','December'];
final year = [
'1900','1901','1902','1903','1904','1905',];
Row(
children: [
//Start Month
Container(
margin: EdgeInsets.only(
top: 4,
bottom: 10,
left: 26,
),
width: 170,
padding:
EdgeInsets.symmetric(horizontal: 12, vertical: 4),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
border:
Border.all(color: Colors.grey.shade700, width: 1.2),
),
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
hint: Text(
'Choose Month',
style: TextStyle(
fontSize: 17,
color: Colors.black,
fontWeight: FontWeight.bold),
),
value: selectedMonthStart,
isExpanded: true,
iconSize: 30,
icon: Icon(
Icons.arrow_drop_down,
color: Colors.black,
),
items: month.map(buildMenuItem).toList(),
onChanged: (value) => setState(() {
this.selectedMonthStart = value;
}),
),
),
),
//Start Year
Container(
margin: EdgeInsets.only(
top: 4,
bottom: 12,
left: 10,
),
width: 120,
padding: EdgeInsets.symmetric(
horizontal: 12,
vertical: 4,
),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(12),
border:
Border.all(color: Colors.grey.shade700, width: 1.2),
),
child: DropdownButtonHideUnderline(
child: DropdownButton<String>(
hint: Text(
'Choose Year',
style: TextStyle(
fontSize: 17,
color: Colors.black,
fontWeight: FontWeight.bold),
),
value: selectedYearStart,
isExpanded: true,
iconSize: 30,
icon: Icon(
Icons.arrow_drop_down,
color: Colors.black,
),
items: year.map(buildMenuItem).toList(),
onChanged: (value) => setState(() {
this.selectedYearStart = value;
}),
),
),
),
],
),
DropdownMenuItem<String> buildMenuItem(String item) => DropdownMenuItem(
value: item,
child: Text(
item,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
);
then this is when i try to make the two Strings into date time
try {
String date1 = selectedMonthStart! + selectedYearStart.toString();
print(date1);//print date1
DateFormat datestart = DateFormat.yMMMM(date1); // this should be the problem?
print(datestart);
String date2 = selectedMonthEnd! + selectedYearEnd.toString();
print(date2);
DateFormat dateend = DateFormat.yMMMM(date2);
print(dateend);
print(FirebaseAuth.instance.currentUser?.uid);
await FirebaseFirestore.instance
.collection("education")
.doc(FirebaseAuth.instance.currentUser!.uid)
.set({
"uid": FirebaseAuth.instance.currentUser?.uid,
"College": collegeAheadController.text,
"imageCollege": imageuniversitycollege,
"Major": SelectedMajor,
"Degree": SelectedDegree,
"StartDate": datestart,
"EndDate": dateend,
}).then((value) => print("Data changed successfully"));
} on FirebaseException catch (e) {
Utils.showSnackBar(e.message);
}
After combining them and make the format date, i want to save it to firstore database
but get this error instead
2
Answers
Init the intl in the primary method
Make sure you import the valid file
You can convert the date format from string, here it is:
Output:
The solution