'intl package and date formatting strange behaviour
I start to use intl package in my dart project. After start to use this package i use this code:
DateTime now = new DateTime.now();
var formatter = new DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
String nowFormatted = formatter.format(now);
And it works correctly. After i use intl i obtain this message in log:
Uncaught LocaleDataException: Locale data has not been initialized, call initializeDateFormatting(<locale>).
I cannot understand why i should pass locale in this code snippet
Solution 1:[1]
intl: ^0.15.7
I've the same issue to the current Intl version so I've solved with
these imports:
import 'package:intl/intl.dart';
import 'package:intl/date_symbol_data_local.dart';
and the code:
initializeDateFormatting();
DateTime now = DateTime.now();
var dateString = DateFormat('dd-MM-yyyy').format(now);
final String configFileName = 'lastConfig.$dateString.json';
Solution 2:[2]
Verify your imports:
import 'package:intl/date_symbol_data_local.dart';
import 'package:intl/intl.dart';
Set initializeDateFormatting according to your language, example:
initializeDateFormatting('pt_BR', null);
Solution 3:[3]
If you encounter this problem, write initializeDateFormatting('az'); top of "Material App". I searched for 1 hour and nobody wrote it clearly.
Solution 4:[4]
I have solved this use in this way:
DateTime now = new DateTime.now();
var formatter = new DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", 'en');
String nowFormatted = formatter.format(now);
But I have to make this to my dart file used to configure itnl support:
library translation_helper;
import 'dart:async';
import 'package:intl/date_symbol_data_local.dart';
import '../../resources/messages_all.dart';
void setupLanguage(){
//var germanDatesFuture = initializeDateFormatting('de_DE', null);
var enDatesFuture = initializeDateFormatting('en', null);
var germanMessagesFuture = initializeMessages('de');
var englishMessagesFuture = initializeMessages('en');
var italianMessagesFuture = initializeMessages('it');
var polishMessagesFuture = initializeMessages('pl');
Future
.wait([
enDatesFuture,
germanMessagesFuture,
englishMessagesFuture,
italianMessagesFuture,
polishMessagesFuture
]);
}
Before I was missing:
var enDatesFuture = initializeDateFormatting('en', null);
For more info I use:
- dart 1.15.0
- intl 0.12.7
Solution 5:[5]
Use this function in main
initializeDateFormatting();
and import like this
import 'package:intl/date_symbol_data_local.dart';
Solution 6:[6]
In your pubspec.yaml, add this dependencie package:
intl:In your highest StatefulWidget (in your dart file), add these imports:
import 'package:intl/intl.dart';
import 'package:intl/date_symbol_data_local.dart';
- In its State, override initState add :
@override
void initState() {
super.initState();
initializeDateFormatting(); //very important
}
- And the code:
DateTime now = new DateTime.now();
var formatter = new DateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
String nowFormatted = formatter.format(now);
Solution 7:[7]
My issue was that I was getting error specifically for the "en_US" Locale even when I was not particularly using it. But I solved it by:
initializeDateFormatting('en', null);
initializeDateFormatting('en_US,', null);
Solution 8:[8]
There's no need to call initializeDateFormatting directly from your code. Just call load method of app's localization delegates.
So you specify delegates like this:
final localizationsDelegates = <LocalizationsDelegate>[
AppLocalizationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
CupertinoLocalizationsDelegate()
];
...
MaterialApp(
localizationsDelegates: localizationsDelegates,
)
And pre-load them with system's locale:
import 'dart:ui' as ui;
...
for (final delegate in localizationsDelegates) {
await delegate.load(ui.Locale(ui.window.locale.languageCode));
}
Solution 9:[9]
In your class with MaterialApp add this code
import 'package:intl/date_symbol_data_local.dart';
@override
void initState() {
// TODO: implement initState
super.initState();`enter code here`
initializeDateFormatting();
}
Solution 10:[10]
I got the same error. But, only when switching between locales ('en' and 'si') within the app. None of the other solutions worked for me. So I just came up with this dumbest solution, and it worked. Just wrapped date-formatting in a try-catch block while initializeDateFormatting() is being called inside the catch block
String date(DateTime date, String _languageCode) {
try {
var formatter = new DateFormat.yMMMMd(_languageCode);
return formatter.format(date);
} catch(e) {
initializeDateFormatting();
var formatter = new DateFormat.yMMMMd(_languageCode);
return formatter.format(date);
}
}
Solution 11:[11]
To initialize the date format as per the system locale.
import 'package:intl/intl_standalone.dart'; // For standlone app
import 'package:intl/intl_browser.dart'; // For standlone app
import 'package:intl/date_symbol_data_local.dart';
...
await initializeDateFormatting(await findSystemLocale(), null);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow

