2

I have the following code running on Flutter:

void main() async {
  await initializeDateFormatting();
...

 () async {
   await findSystemLocale().then((locale) {
     print(locale);
   });
   print(DateFormat.yMMMMd().format(DateTime.now()));
   print(DateFormat.MMMMd().format(DateTime.now()));   
 },

And I get the follwing output on my Android 9 Emulator:

I/flutter ( 4239): pt_BR
I/flutter ( 4239): January 9, 2020
I/flutter ( 4239): January 9
I/flutter ( 4239): Tap on a Card!

It's detecting correctly that the system is configured as pt_BR (Portuguese/Brazil), but it's output is as if it was en_US.

I could swear that changing the system's language was automatically changing the DateFormat output, but now it doesn't anymore and I didn't upgrade the code, neither change it. I already tried to restart the device, reinstall the app, but nothing changes.

I know I can force the locale into the DateFormat but I just want to learn on how to do it right and what am I doing wrong.

If I do this instead:

void main() async {
  await initializeDateFormatting();
  Intl.defaultLocale = await findSystemLocale();

Hard setting the defaultLocale on initialization, it works as expected, so I am guessing the defaultLocale isn't being loaded automatically. This might have to do with the fact that I am running my code inside a new Zone with:

  await initializeDateFormatting();
  Intl.defaultLocale = await findSystemLocale();

  runZoned(
    () => runApp(MyApp()),
    onError: Crashlytics.instance.recordError,
    zoneSpecification: ZoneSpecification(
      print: (self, parent, zone, message) {
        if (isDebug) { // we only print in a debug build.
          parent.print(zone, message);
        }
      },
    ),
  );

If so, how to fix this?

Michel Feinstein
  • 13,416
  • 16
  • 91
  • 173

0 Answers0