必须初始化不可为空的实例字段“_localizedStrings”
Posted
技术标签:
【中文标题】必须初始化不可为空的实例字段“_localizedStrings”【英文标题】:Non-nullable instance field '_localizedStrings' must be initialized 【发布时间】:2021-10-11 12:03:43 【问题描述】:说实话,我以前没有遇到过这种错误。我正在关注您可以在此处看到的教程: https://resocoder.com/2019/06/01/flutter-localization-the-easy-way-internationalization-with-json/
这是我的代码: MAIN.dart:
import 'app_localizations.dart';
void main()
runApp(MyApp());
class MyApp extends StatelessWidget
// This widget is the root of your application.
@override
Widget build(BuildContext context)
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
supportedLocales: [Locale('en', 'US'), Locale('vi', 'VN')],
localizationsDelegates: [
// THIS CLASS WILL BE ADDED LATER
// A class which loads the translations from JSON files
AppLocalizations.delegate,
// Built-in localization of basic text for Material widgets
GlobalMaterialLocalizations.delegate,
// Built-in localization for text direction LTR/RTL
GlobalWidgetsLocalizations.delegate,
],
localeResolutionCallback: (locale, supportedLocales)
for (var supportedLocale in supportedLocales)
if (supportedLocale.languageCode == locale!.languageCode &&
supportedLocale.countryCode == locale.countryCode)
return supportedLocale;
return supportedLocales.first;
,
home: MyHomePage(),
);
APP_LOCALIZATIONS.dart:
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class AppLocalizations
final Locale locale;
//error right here
AppLocalizations(this.locale);
//error right here
//modify
static AppLocalizations? of(BuildContext context)
return Localizations.of<AppLocalizations>(context, AppLocalizations);
//modify
static const LocalizationsDelegate<AppLocalizations> delegate =
_AppLocalizationsDelegate();
Map<String, String> _localizedStrings;
Future<bool> load() async
String jsonString =
await rootBundle.loadString('lang/$locale.languageCode.json');
Map<String, dynamic> jsonMap = json.decode(jsonString);
_localizedStrings = jsonMap.map((key, value)
return MapEntry(key, value.toString());
);
return true;
//modify
String? translate(String key)
return _localizedStrings[key];
//modify
class _AppLocalizationsDelegate
extends LocalizationsDelegate<AppLocalizations>
const _AppLocalizationsDelegate();
@override
bool isSupported(Locale locale)
return ['en', 'vi'].contains(locale.languageCode);
@override
Future<AppLocalizations> load(Locale locale) async
AppLocalizations localizations = new AppLocalizations(locale);
await localizations.load();
return localizations;
@override
bool shouldReload(_AppLocalizationsDelegate old) => false;
这里的一些代码由于一些错误而被修改,所以它与教程有点不同(修复错误。)。所以也许这就是原因。我在上面对那个修改发表了一些评论。
P/S:我尝试寻找关于此的其他答案,但我无法理解,所以请为我解释!
【问题讨论】:
【参考方案1】:只需将_localizedStrings
初始化为一个空地图。
Map<String, String> _localizedStrings = ;
这样即使还没有数据,它也永远不会为空。
另一种选择是在声明之前添加late
修饰符,告诉编译器稍后将对其进行初始化。
late Map<String, String> _localizedStrings;
【讨论】:
以上是关于必须初始化不可为空的实例字段“_localizedStrings”的主要内容,如果未能解决你的问题,请参考以下文章
必须初始化不可为空的实例字段“_localizedStrings”