我正在使用easy_localization
来本地化Flutter应用程序(flutter stable 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/
我有什么遗漏吗?我应该怎么做来解决这个问题?