localizely / flutter-intl-vscode

This VS Code extension generates boilerplate code for localization of Flutter apps with official Dart Intl library
MIT License
87 stars 1 forks source link

The asynchronous future call inside the generated S.load method causes an instantaneous black screen #76

Closed Racinggavin closed 1 year ago

Racinggavin commented 3 years ago

my observation as the github repository here

Racinggavin commented 3 years ago

I just resolved the problem by manually modify the generated files. It's the latest commit of my github repro.

/lib/generated/l10n.dart

S _lookupS(Locale locale) {
  final name = (locale.countryCode?.isEmpty ?? false)
      ? locale.languageCode
      : locale.toString();
  String localeName = Intl.canonicalizedLocale(name);
  if(!initializeMessagesSync(localeName)) {
    localeName = 'en';
  }
  Intl.defaultLocale = localeName;
  final instance = S();
  S._current = instance;
  return instance;
}

static Future<S> load(Locale locale) {
  return SynchronousFuture<S>(_lookupS(locale));
}

/lib/generated/intl/messages_all.dart

bool initializeMessagesSync(String localeName) {
  var availableLocale = Intl.verifiedLocale(
      localeName, (locale) => _deferredLibraries[locale] != null,
      onFailure: (_) => null);
  if (availableLocale == null) {
    return false;
  }
  initializeInternalMessageLookup(() => new CompositeMessageLookup());
  messageLookup.addLocale(availableLocale, _findGeneratedMessagesFor);
  return true;
}
lzoran commented 3 years ago

Hi @Racinggavin,

Thanks for reporting this!

I've checked the attached sample, and you are right. Indeed there is some strange behavior (black screen) during the async load of localization messages.

According to the issue, it seems that this has something with the MaterialApp and its implementation of loading localizations.

Regarding the proposed modification, it will work well in case the use_deferred_loading is disabled (default behavior). However, I'm not sure that it will work for others that have this setting enabled. Anyway, thanks for the workaround until this issue is resolved.

doppio commented 2 years ago

@lzoran, since it doesn't look like https://github.com/flutter/flutter/issues/87926 is receiving much attention, would you consider incorporating @Racinggavin's workaround as part of the code generation (maybe as an opt-in setting)? It's easy to forget to manually update these files every time they get regenerated.

lzoran commented 1 year ago

This should be fixed with the Flutter Intl 1.19.0 extension and the intl_utils 2.8.1 package that is used under the hood for generating the localization files.

I'm closing this issue as resolved. In case you detect any irregularity, feel free to reopen it.