延迟初始化错误:使用easy_localization来国际化颤振应用程序时,字段"_deviceLocale@66168148"尚未初始化



我使用easy_localization来定位颤振应用程序(颤振稳定2.5.3(,但当我运行该应用程序时,显示如下错误:

======== 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)
====================================================================================================

为了重现这个问题,我做了一个简单的例子。这是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(),
));
}

这就是language_util.dart文件:

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);
}

这是r.dart文件:

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';
}
}

这就是yaml定义:

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/

我是不是错过了什么?我该怎么办才能解决这个问题?

您忘记初始化easy_localization(官方示例(。你需要打电话给

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

在创建EasyLocalization小部件之前。根据文件,WidgetsFlutterBinding

A concrete binding for applications based on the Widgets framework.
This is the glue that binds the framework to the Flutter engine.

例如,获取设备区域设置需要这样做,这是通过EasyLocalization初始化完成的。

相关内容

最新更新