我有 2 个下拉列表,在用户选择列表之一后从列表的字符串返回字符串变量,以及一个处理更新数据到 firestore 的按钮。
这是我创建的变量
String? selectedMonthEnd;
String? selectedYearEnd;
Run Code Online (Sandbox Code Playgroud)
这是2个下拉菜单的代码
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,
),
),
);
Run Code Online (Sandbox Code Playgroud)
然后这是我尝试将两个字符串转换为日期时间的时候
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);
}
Run Code Online (Sandbox Code Playgroud)
将它们组合并设置日期格式后,我想将其保存到firstore数据库,但出现此错误
Mok*_*man 16
在primary方法中初始化intl
initializeDateFormatting();
Run Code Online (Sandbox Code Playgroud)
确保导入有效的文件
import 'package:intl/date_symbol_data_local.dart';
Run Code Online (Sandbox Code Playgroud)
您可以将日期格式从字符串转换为:
String date1 = "$selectedMonthStart $selectedYearStart";
final startDate = DateFormat.yMMMM(/*you can pass here local*/).parse(date1);
Run Code Online (Sandbox Code Playgroud)
输出:
1902-04-01 00:00:00.000
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10896 次 |
| 最近记录: |