gpt4 book ai didi

localization - Flutter 中的本地化字符串

转载 作者:IT王子 更新时间:2023-10-29 06:58:55 24 4
gpt4 key购买 nike

我正在构建一个演示应用程序以使用本地化字符串进行测试。我收到以下错误:

I/flutter (21588):构建 MainApp(脏)时抛出以下 NoSuchMethodError:I/flutter (21588): getter 'title' 被调用为 null。I/flutter (21588): 接收器: nullI/flutter (21588): 尝试调用: title

我不确定为什么会收到此错误。我已按照 flutter 文档中的指示进行操作。

我有以下本地化类:

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'dart:async';
import 'package:bet_master/l10n/messages_all.dart';

class AppLocalizations {
static Future<AppLocalizations> load(Locale locale) {
final String name =
locale.countryCode.isEmpty ? locale.languageCode : locale.toString();
final localeName = Intl.canonicalizedLocale(name);

return initializeMessages(localeName).then((bool _) {
Intl.defaultLocale = localeName;
return AppLocalizations();
});
}
static AppLocalizations of(BuildContext context) {
return Localizations.of<AppLocalizations>(context, AppLocalizations);
}

String get title {
return Intl.message(
'Bet Master',
name: 'title',
desc: 'App Title'
);
}

String get search {
return Intl.message(
'Search',
name: 'search',
desc : ''
);
}

}

class AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations> {
const AppLocalizationsDelegate();

@override
bool isSupported(Locale locale) {
return ['en', 'es', 'fr'].contains(locale.languageCode);
}

@override
Future<AppLocalizations> load(Locale locale) {
return AppLocalizations.load(locale);
}

@override
bool shouldReload(AppLocalizationsDelegate old) {
return false;
}
}

对于主页小部件,我只设置标题

import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:bet_master/localization/localizations.dart';

void main() {
runApp(MainApp());
}

class MainApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
// TODO: implement build
return MaterialApp(
localizationsDelegates: [
const AppLocalizationsDelegate(),
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate
],
supportedLocales: [
const Locale('en', ''),
const Locale('es', ''),
const Locale('fr', ''),
],
home: Scaffold(
appBar: AppBar(
title: Text(AppLocalizations.of(context).title),
),
),
);
}
}

最佳答案

最后,问题似乎与本地化不可用有关,我将“主页”代码移至另一个小部件并解决了该问题。

Widget build(BuildContext context) {
return new MaterialApp(
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
_appLocalizationsDelegate,
],
supportedLocales: [
Locale('en'),
Locale('es'),
],
locale: _appLocalizationsDelegate.overridenLocale,
onGenerateTitle: (BuildContext context) => AppLocalizations.of(context).title,
home: Home()
);
}
}

class Home extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
title: Text(AppLocalizations.of(context).title),
),
body: Column(children: <Widget>[
Text('hola'),
],)
);
}
}

我仍然需要研究为什么需要这样做,但至少它现在有效。

关于localization - Flutter 中的本地化字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54210254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com