47

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

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Andrea Bozza
  • 1,374
  • 2
  • 12
  • 31

12 Answers12

55

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';
Community
  • 1
  • 1
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
23

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);
rubStackOverflow
  • 5,615
  • 2
  • 29
  • 43
22

If you encounter this problem, write initializeDateFormatting('az'); top of "Material App". I searched for 1 hour and nobody wrote it clearly.

it is really solved. enter image description here

FarZad
  • 463
  • 3
  • 19
Söhrab Vahidli
  • 587
  • 7
  • 10
13

Use this function in main

initializeDateFormatting();

and import like this

import 'package:intl/date_symbol_data_local.dart';

umutckmk
  • 131
  • 2
  • 3
11

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
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
Andrea Bozza
  • 1,374
  • 2
  • 12
  • 31
  • Interesting. Why do you need that while it worked for me without. What OS are you using? – Günter Zöchbauer Mar 23 '16 at 09:56
  • Boring OSX last version -_- – Andrea Bozza Mar 23 '16 at 10:01
  • 3
    Yes, you have to initialize the format for a particular locale before you can use it. In some cases the data is already loaded, but in others (most commonly messages, where the data can take up a lot of space) it might need to load the data before it can be used. Typically you wouldn't initialize all the messages for every locale, just the one for your user's locale. – Alan Knight Mar 24 '16 at 17:33
  • 1
    Where do you put the configuration? – Giraldi Oct 30 '18 at 10:11
6

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));
}
Igor Kharakhordin
  • 9,185
  • 3
  • 40
  • 39
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);
B. Go
  • 1,436
  • 4
  • 15
  • 22
5

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);
Ndivhuwo
  • 280
  • 2
  • 10
5

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();
  }
Alex Weitz
  • 3,199
  • 4
  • 34
  • 57
shams7
  • 71
  • 1
  • 3
1

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);
e-j5
  • 1,759
  • 1
  • 11
  • 23
0

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);
    }
}
ruwan800
  • 1,567
  • 17
  • 21
0

Depending on intl: ^0.18.0

Just add this line in the method that listens to each time the locale changes

Intl.defaultLocale = languageCode;

package: https://pub.dev/packages/intl