Dart cannot turn a compact timestamp like 20201101153000 into a DateTime
FormatException: Invalid date formatShort answer
DateTime.parse accepts a subset of ISO 8601, and a time part must be introduced by T or a space. A 14-digit string such as 20201101153000 has no separator, so it throws FormatException. Insert a T after the eighth digit. When bad values such as month 13 must be rejected, add separators and parse strictly with DateFormat from the intl package.
Fix it fast
1Find out which cause you have
Print the exact string you pass to parse, with its length. Compare it with the accepted shapes: 20120227T132700, 2012-02-27 13:27:00.
final s = '20201101153000'; print('${s.length}: $s');2Match what you see, then jump to its fix
What does the error look like?
FormatException: Invalid date format
Cause 1 of 2
No T or space between the date and the time
1Check
The documented formats take the date first, then an optional time that starts with T or a space. Digits running on directly are not that shape.
try {
DateTime.parse('20201101153000');
} on FormatException catch (e) {
print(e);
}2Fix
Insert the T yourself before parsing.
final s = '20201101153000';
final dt = DateTime.parse('${s.substring(0, 8)}T${s.substring(8)}');3Verify
Print the result; it should read 2020-11-01 15:30:00.000.
print(dt);Confirmed by api.dart.dev
Cause 2 of 2
You want a strict, pattern-based parse
1Check
DateTime.parse is not strict: the docs say it accepts out-of-range components and carries the overflow into the next unit, so "2020-01-42" becomes 2020-02-11. Strict validation is the job of DateFormat.parseStrict from the intl package.
flutter pub add intl2Fix
Describe the pattern with DateFormat and parse with it.
import 'package:intl/intl.dart';
final s = '20201101153000';
final spaced = '${s.substring(0, 4)}-${s.substring(4, 6)}-${s.substring(6, 8)} '
'${s.substring(8, 10)}:${s.substring(10, 12)}:${s.substring(12)}';
final dt = DateFormat('yyyy-MM-dd HH:mm:ss').parseStrict(spaced);3Verify
Print the value, then feed it a bad month to confirm it is rejected with an exception.
print(dt);Confirmed by pub.dev
How do I stop it coming back?
- Store timestamps as ISO 8601 strings (with T and a zone) when you control the format.
- Wrap parsing of external data in try/catch and decide what to show when it fails.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
- SessionBirdAn omnichannel engagement suite in Flutter: campaigns, chatbot, tickets, analytics and live calling.
Related fixes
Mobile apps
AudioCache has no play method after updating audioplayers
The method 'play' isn't defined for the type 'AudioCache'.
Since audioplayers 1.0, AudioCache only turns asset files into local files and caches them.
2 causes, each with a fixMobile apps
Flutter Bloc throws that emit was called after the event handler completed
emit was called after an event handler completed normally.
Each on<Event> handler gets its own Emitter, and it stops working the moment the handler's returned Future completes.
2 causes, each with a fixMobile apps
analysis_options.yaml: the URI it includes can't be found
include_file_not_found
The analyzer reports include_file_not_found when the file named after include: in analysis_options.yaml does not resolve.
2 causes, each with a fixMobile apps
Dart error: the body might complete normally and return null for a non-nullable type
body_might_complete_normally
Dart found a path through your function or callback that reaches the closing brace without a return.
3 causes, each with a fixMobile apps
Dart throws ConcurrentModificationError while looping over a list
ConcurrentModificationError
Dart throws this when a collection changes size while something iterates it.
2 causes, each with a fixMobile apps
FormatException: Invalid radix-10 number when parsing a string to int
FormatException: Invalid radix-10 number
int.parse accepts only a whole-number literal.
3 causes, each with a fix