skip to Main Content

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

enter image description here

is there any suggestion what to do?
below is date1
date1

2

Answers


  1. Init the intl in the primary method

     initializeDateFormatting();
    

    Make sure you import the valid file

    import 'package:intl/date_symbol_data_local.dart';
    

    You can convert the date format from string, here it is:

    String date1 = "$selectedMonthStart $selectedYearStart";
    final startDate = DateFormat.yMMMM(/*you can pass here local*/).parse(date1);
    

    Output:

    1902-04-01 00:00:00.000
    
    Login or Signup to reply.
  2. The solution

    import 'package:intl/intl.dart';
    import 'package:intl/date_symbol_data_local.dart';
    
    void main(List<String> args) {
    initializeDateFormatting("ar_SA", null).then((_) {
      print(DateFormat('EEEE, h:mm a','ar_SA').format(DateTime.parse("2022-10-02 23:59:59")));
        });
    print(DateFormat('EEEE, h:mm a','en_US').format(DateTime.parse("2022-10-02 23:59:59")));
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search