flutter 将2个字符串变量合并为1个,并使其成为日期格式的抖动

dgenwo3n  于 2022-12-14  发布在  Flutter
关注(0)|答案(1)|浏览(178)

我有2个下拉按钮,在用户选择列表中的一个后,从列表的字符串返回字符串变量。
这是我创建的变量

String? selectedMonthEnd;
  String? selectedYearEnd;

这是2个下拉菜单的代码

Row(
                  children: [
                    //End Month
                    Container(
                      margin: EdgeInsets.only(bottom: 10, left: 26),
                      width: 170,
                      padding:
                          EdgeInsets.symmetric(horizontal: 12, vertical: 4),
                      child: DropdownButtonHideUnderline(
                        child: DropdownButton<String>(
                          hint: Text(
                            'Choose Month',
                            style: TextStyle(
                                fontSize: 17,
                                color: Colors.black,
                                fontWeight: FontWeight.bold),
                          ),
                          value: selectedMonthEnd,
                          isExpanded: true,
                          iconSize: 30,
                          icon: Icon(
                            Icons.arrow_drop_down,
                            color: Colors.black,
                          ),
                          items: month.map(buildMenuItem).toList(),
                          onChanged: (value) => setState(() {
                            this.selectedMonthEnd = value;
                          }),
                        ),
                      ),
                    ),
                    // End Year
                    Container(
                      margin: EdgeInsets.only(bottom: 12, left: 10),
                      width: 120,
                      padding: EdgeInsets.symmetric( horizontal: 12,vertical: 4,),
                      child: DropdownButtonHideUnderline(
                        child: DropdownButton<String>(
                          hint: Text(
                            'Choose Year',
                            style: TextStyle(
                                fontSize: 17,
                                color: Colors.black,
                                fontWeight: FontWeight.bold),
                          ),
                          value: selectedYearEnd,
                          isExpanded: true,
                          iconSize: 30,
                          icon: Icon(
                            Icons.arrow_drop_down,
                            color: Colors.black,
                          ),
                          items: year.map(buildMenuItem).toList(),
                          onChanged: (value) => setState(() {
                            this.selectedYearEnd = value;
                          }),
                        ),
                      ),
                    ),
                  ],
                ),

这就是我正在尝试做的,将它们组合起来,转换成日期时间格式,然后将它们保存到Firestore数据库中。
这是将它们保存在firestore数据库中的代码

try {
      String date1 = selectedMonthStart! + selectedYearStart.toString();//combining 2 strings
      DateFormat datestart = DateFormat.yMMMM(date1);//make them a date
      print(datestart);
      String date2 = selectedMonthEnd! + selectedYearEnd.toString();//combining
      DateFormat dateend = DateFormat.yMMMM(date2); //make them a date
      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);
    }

我想将下拉列表中的2个字符串转换为日期格式,然后将它们添加到firestore数据库中

g52tjvyc

g52tjvyc1#

该参数接受一个字符串,但您添加了[date2],它等效于字符串列表:
试试这个:

DateFormat dateend = DateFormat.yMMMM(date2);

相关问题