Home > OS >  LateInitializationError: Field '_deviceLocale@66168148' has not been initialized when use
LateInitializationError: Field '_deviceLocale@66168148' has not been initialized when use

Time:11-05

I am using easy_localization to localization the flutter app(flutter stable 2.5.3), but when I run the app, shows error like this:

======== Exception caught by widgets library =======================================================
The following LateError was thrown attaching to the render tree:
LateInitializationError: Field '_deviceLocale@66168148' 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)
#5      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4805:57)
#6      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4638:5)
#7      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3673:14)
#8      Element.updateChild (package:flutter/src/widgets/framework.dart:3425:18)
#9      RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1198:16)
#10     RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1167:5)
#11     RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1112:18)
#12     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2573:19)
#13     RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1111:13)
#14     WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:944:7)
#15     WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:924:7)
(elided 11 frames from class _RawReceivePortImpl, class _Timer, dart:async, and dart:async-patch)
====================================================================================================

To reproduce the problem, I made a minimal example. this is the main.dart:

import 'package:easy_localization/easy_localization.dart';
import 'package:easy_localization_loader/easy_localization_loader.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn/utilities/language_util.dart';

    void main() {
      runApp(EasyLocalization(
        supportedLocales: [
          Locale(kLanguageEN),
          Locale(kLanguageZH),
        ],
        path: 'assets/translations',
        assetLoader: YamlAssetLoader(),
        fallbackLocale: Locale(kLanguageEN),
        child: Container(),
      ));
    }

and this is the language_util.dart file:

import 'package:easy_localization/src/public_ext.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn/utilities/r.dart';


const kLanguageDE = 'de';
const kLanguageEN = 'en';
const kLanguageES = 'es';
const kLanguageFR = 'fr';
const kLanguageIT = 'it';
const kLanguageJA = 'ja';
const kLanguageKO = 'ko';
const kLanguagePT = 'pt';
const kLanguageRU = 'ru';
const kLanguageZH = 'zh';

final List<String> kAppLanguages = [
  kLanguageEN,
  kLanguageZH,
];

final List<String> kSupportedLanguages = [
  kLanguageDE,
  kLanguageEN,
  kLanguageES,
  kLanguageFR,
  kLanguageIT,
  kLanguageJA,
  kLanguageKO,
  kLanguagePT,
  kLanguageRU,
  kLanguageZH,
];

final Map<String, String> _knownFlagIcons = {
  kLanguageDE: 'de',
  kLanguageEN: 'gb',
  kLanguageES: 'es',
  kLanguageFR: 'fr',
  kLanguageIT: 'in',
  kLanguageJA: 'jp',
  kLanguageKO: 'kr',
  kLanguagePT: 'pt',
  kLanguageRU: 'ru',
  kLanguageZH: 'cn',
};

String getLanguageName(String language) {
  return 'language.$language'.tr();
}

String getLanguageFlag(String language) {
  return R.image('flag_icons/${_knownFlagIcons[language]}.svg');
}

Locale languageToLocale(String language) {
  if (language.contains('-')) {
    return Locale(
        language.substring(0, 1).toUpperCase(), language.substring(1));
  }
  return Locale(language);
}

this is the r.dart file:

import 'package:flutter/material.dart';

class R {
  static GlobalKey<NavigatorState> _navigatorKey;

  static setNavigatorKey(GlobalKey navigatorKey) {
    _navigatorKey = navigatorKey;
  }

  static GlobalKey<NavigatorState> getNavigatorKey() {
    return _navigatorKey;
  }

  static String image(String name) {
    return 'assets/images/$name';
  }
}

and this is the yaml define:

name: flutter_learn
description: A new Flutter application.
publish_to: 'none' 
version: 1.0.0 1

environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  easy_localization: ^3.0.0
  easy_localization_loader: ^1.0.0
  cupertino_icons: ^1.0.0

flutter:
  uses-material-design: true

  assets:
    - assets/translations/

am I missing something? what should I do to fix this problem?

CodePudding user response:

You forgot initialize easy_localization (official example). You need to call

WidgetsFlutterBinding.ensureInitialized();
await EasyLocalization.ensureInitialized();

before creating your EasyLocalization widget. The WidgetsFlutterBinding is, according to the docs

A concrete binding for applications based on the Widgets framework.

This is the glue that binds the framework to the Flutter engine.

That is needed to, for example, get the device locale, which is done by the EasyLocalization initialization.

  • Related