I have an user who have this error :

Fatal Exception: io.flutter.plugins.firebase.crashlytics.FlutterError: FormatException: Invalid date format
. Error thrown null.
       at DateTime.parse(DateTime.java)

The error come from this part of my code

String myString ="";

  void myfunction() async{
    SharedPreferences prefs = await SharedPreferences.getInstance();

    String myString = prefs.getString("key")??"";

    _variable = myString!=null||myString!="" ? DateTime.parse("$myString") : DateTime.now();
  }

firebase crash point here

_variable = myString!=null||myString!="" ? DateTime.parse("$myString") : DateTime.now();

I don't understand because the error seems to found a null but my cause prevent null by forcing null to be DateTime.now();

I'm wrong ?


Solution 1: pmatatias

DateTime parse(String formattedString)

Constructs a new DateTime instance based on formattedString. Throws a FormatException if the input string cannot be parsed. To detect and reject invalid component values, use DateFormat.parseStrict from the intl package.

more details on documentation

maybe you can try like below:

// convert data to string
_variable = data!=null ? DateTime.parse('$data') : DateTime.now();


Solution 2: Just a Person

DateTime.parse method accepts a string as an input but the input which is passed to the method must be a valid one. If the input is invalid it throws FormatException.

From the docs:

The accepted inputs are currently:

  • A date: A signed four-to-six digit year, two digit month and two digit day, optionally separated by - characters. Examples: "19700101", "-0004-12-24", "81030-04-01".

  • An optional time part, separated from the date by either T or a space. The time part is a two digit hour, then optionally a two digit minutes value, then optionally a two digit seconds value, and then optionally a '.' or ',' followed by at least a one digit second fraction. The minutes and seconds may be separated from the previous parts by a ':'. Examples: "12", "12:30:24.124", "12:30:24,124", "123010.50".

  • An optional time-zone offset part, possibly separated from the previous by a space. The time zone is either 'z' or 'Z', or it is a signed two digit hour part and an optional two digit minute part. The sign must be either "+" or "-", and cannot be omitted. The minutes may be separated from the hours by a ':'. Examples: "Z", "-10", "+01:30", "+1130".

This includes the output of both toString and toIso8601String, which will be parsed back into a DateTime object with the same time as the original.

The result is always in either local time or UTC. If a time zone offset other than UTC is specified, the time is converted to the equivalent UTC time.

Examples of accepted strings:

"2012-02-27" "2012-02-27 13:27:00" "2012-02-27 13:27:00.123456789z" "2012-02-27 13:27:00,123456789z" "20120227 13:27:00" "20120227T132700" "20120227" "+20120227" "2012-02-27T14Z" "2012-02-27T14+00:00" "-123450101 00:00:00 Z": in the year -12345. "2002-02-27T14:00:00-0500": Same as "2002-02-27T19:00:00Z"

Check the format of the String coming from your api and see if it matches the given format above. Can you post an example string which you get from firebase, so as to further check?


Solution 3: Mahesh

In this case Null condition comes TRUE so that result converting date formate is Empty. so please check this once

// here always empty data will assign. 
String myString = prefs.getString("key")??"";

DateTime _variable =  myString != "" ? DateTime.parse(myString) : DateTime.now();

print(_variable);


Solution 4: Sheychan

It's very likely that your string in this code has invalid/empty string. And you are using wrong operation "||" as well.

_variable = myString!=null||myString!="" ? DateTime.parse("$myString") : DateTime.now();

But for a better approach, if your intention is simply replacing the date into current date in spite of parse exceptions, I suggest you do DateTime.tryParse

 _variable = DateTime.tryParse(myString) ?? DateTime.now();


Solution 5: Kaushik Chandru

For example the value is empty but not null the condition

myString!=null||myString!="" 

Is true since it satisfies the condition that its not null. You have to check if not null AND not empty so using && should solve the issue

_variable = myString!=null && myString!="" ? DateTime.parse("$myString") : DateTime.now();

Also it's better to write

_variable = DateTime.tryParse('$myString') ?? DateTime.now();

This will try to parse the string and if it returns null then it will assign datetime. Now


Solution 6: L. Gangemi

The error comes from a wrong handling of the possible NULL value.

In this part of the code you are reading a value from SharedPreferences and you implemented a check wether the value is null or not. In case the value is null, myString is equal to empty string. That means that whatever happens, myString cannot be null.

  String myString = prefs.getString("key")??"";

Then you want to load _variable with the date value.

  • If myString has the date value, then _variable should contain that value parsed
  • If myString is null or contains an empty string value "" then _variable should contain DateTime.now().
    _variable = myString!=null||myString!="" ? DateTime.parse("$myString") : DateTime.now();

The error is in the ternary operator condition:

    myString!=null||myString!=""

As I wrote in before: myString cannot be null.

That means that the first check inside the condition myString!=null will always be True, and since the checks are in OR relation, the whole condition is gonna be True. It doesn't matter if myString!="".

This resolves in the exectution of the positive branch of the ternary operator: DateTime.parse("$myString").

So, if myString is equal to "", then this gets executed DateTime.parse("") and the error is thrown.

Here's a possible solution:

    String myString = prefs.getString("key")??"";
    _variable = myString!="" ? DateTime.parse("$myString") : DateTime.now();


Solution 7: Guillaume Roux

This exception occurs when the string you're passing to DateTime.parse is not a valid one. You can consider using DateTime.tryParse which will return null instead of throwing a FormatException when the string is not valid.

final String myString = prefs.getString("key") ?? "";

final DateTime _variable = DateTime.tryParse(myString) ?? DateTime.now();