Performance 与Flatter中继承的小部件相比,重新加载整个应用程序是否会有任何性能损失
我正在构建一个本地化应用程序。我需要在不想传递上下文的地方访问本地化,以便使用(BuildContext)的AppLocalizations。因此,我在AppLocalizations中添加了BehaviorSubject,并在main.ts中收听它,main.ts用StatefullWigdet包装。在事件上,我触发setState((){});这样所有的Wigdet都能得到新的翻译。这样我就不需要做AppLocalizations.of(context).translate('loginButton_text')我可以做AppLocalizations.translate('loginButton_text')。我觉得InheritedWidget方法应该更出色,但在检查性能选项卡时,我没有注意到任何明显的CPU性能差异 你认为我的方法有什么缺点吗 代码如下所示: 应用本地化类Performance 与Flatter中继承的小部件相比,重新加载整个应用程序是否会有任何性能损失,performance,flutter,localization,Performance,Flutter,Localization,我正在构建一个本地化应用程序。我需要在不想传递上下文的地方访问本地化,以便使用(BuildContext)的AppLocalizations。因此,我在AppLocalizations中添加了BehaviorSubject,并在main.ts中收听它,main.ts用StatefullWigdet包装。在事件上,我触发setState((){});这样所有的Wigdet都能得到新的翻译。这样我就不需要做AppLocalizations.of(context).translate('loginBu
static var translationsLoaded = new BehaviorSubject();
Future<bool> load() async {
String jsonString = await rootBundle.loadString(assets/lang/${locale.languageCode}.json');
_localizedStrings = json.decode(jsonString);
translationsLoaded.add('');
return true;
}
static String translate(String key){
return _localizedStrings[key];
}
Text(AppLocalizations.translate('key'));
获取翻译
static var translationsLoaded = new BehaviorSubject();
Future<bool> load() async {
String jsonString = await rootBundle.loadString(assets/lang/${locale.languageCode}.json');
_localizedStrings = json.decode(jsonString);
translationsLoaded.add('');
return true;
}
static String translate(String key){
return _localizedStrings[key];
}
Text(AppLocalizations.translate('key'));