zHaytam / EasyLocalization

A simple library that makes WPF Localization easier
MIT License
22 stars 6 forks source link

lateinit _deviceLocale not initialized on startup #3

Closed ChemoCosmo closed 3 years ago

ChemoCosmo commented 3 years ago

After upgrading the Dart-SDK to null-safety and upgrading EasyLocalisation to version 3.0.0 in the process my app crashes on startup with:

I/flutter ( 6519): [🌎 Easy Localization] [DEBUG] Start
I/flutter ( 6519): [🌎 Easy Localization] [DEBUG] Init state

======== Exception caught by widgets library =======================================================
The following LateError was thrown attaching to the render tree:
LateInitializationError: Field '_deviceLocale@241168148' has not been initialized.

When the exception was thrown, this was the stack: 
#0      EasyLocalizationController._deviceLocale (package:easy_localization/src/easy_localization_controller.dart)
#1      new EasyLocalizationController.<anonymous closure> (package:easy_localization/src/easy_localization_controller.dart:52:48)
#2      ListMixin.firstWhere (dart:collection/list.dart:161:15)
#3      new EasyLocalizationController (package:easy_localization/src/easy_localization_controller.dart:51:34)
#4      _EasyLocalizationState.initState (package:easy_localization/src/easy_localization_app.dart:120:30)
...
====================================================================================================

My main.dart looks as follows:

[...]
void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(EasyLocalization(
      supportedLocales: [
        Locale('en'),
        Locale('de'),
        Locale('ru'),
        Locale('fr')
      ],
      path: 'none',
      assetLoader: ConstJsonAssetLoader(),
      fallbackLocale: Locale('en'),
      child: MyApp()));
}
[...]

ConstJsonAssetLoader is just a costum asset loader that reads translations from constants instead of parsing files for performance reasons (hence the path: 'none'). I do not think that is the issue here.

The problem occurs in the Android Emulator 30.5.3-7196367 with an Android 11.0 (R) - API 30 Nexus 5X device. Default locale there is de-US.

A workaround is the usage of startLocale-Parameter but I'd rather use device locale.

ChemoCosmo commented 3 years ago

Nevermind, ensureInitialized does the trick. A little bit nasty to run the main method asynchronously though.

void main() async {
  await EasyLocalization.ensureInitialized();
  runApp(EasyLocalization(
  [...]

Remove this issue at will. It might be helpful to others though to find this (new) error message and its solution.

nijatmursali commented 3 years ago

Why authors did not mention this one? I did your trick and it worked thanks.

manasWecreate commented 3 years ago

@ChemoCosmo Thank you! that worked :) @nijatmursali The author added this in 3.0.0 release https://pub.dev/packages/easy_localization/changelog

kaangms commented 3 years ago

It can be edited in addition to the @ChemoCosmo so that it does not give a null error

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await EasyLocalization.ensureInitialized();
  runApp(EasyLocalization(
  [...]