Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 516
Andrea Bozza Avatar asked Mar 23 '16 09:03

Andrea Bozza


4 Answers

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';
like image 114
Alessandro Ornano Avatar answered Nov 09 '22 23:11

Alessandro Ornano


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);
like image 36
rubStackOverflow Avatar answered Nov 10 '22 00:11

rubStackOverflow


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

like image 15
Söhrab Vahidli Avatar answered Nov 09 '22 23:11

Söhrab Vahidli


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
like image 11
Andrea Bozza Avatar answered Nov 10 '22 01:11

Andrea Bozza