Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Locale data has not been initialized, call initializeDateFormatting(<locale>), combining 2 variable strings as datetime

Tags:

flutter

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

like image 868
Jessen Jie Avatar asked Dec 12 '25 10:12

Jessen Jie


2 Answers

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
like image 181
Moklesur Rahman Avatar answered Dec 15 '25 04:12

Moklesur Rahman


import 'package:intl/date_symbol_data_local.dart';
    
    void main() {
      initializeDateFormatting('pt_BR', null).then((_) => runApp(const myApp()));
    }

I used this solution in my code and it works. I found this in documentation of DateFormat class enter link description here

like image 26
Yara Sá Avatar answered Dec 15 '25 05:12

Yara Sá



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!