我是Flutter本地化的新手。我正在尝试在抖动的IOS应用中添加它,但是出现以下错误:
方法“翻译”在null上被调用。 接收者:null 尝试调用:translate(“ menu”)
我希望有人可以帮我解决这个问题-我已经尝试了好几个小时,但无法解决问题-希望你能提供帮助
主要:
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:xobrew_app/services/app_localizations.dart';
class forside extends StatefulWidget {
@override
_forsideState createState() => _forsideState();
}
class _forsideState extends State<forside> {
@override
Widget build(BuildContext context) {
return MaterialApp(
supportedLocales: [
Locale('en', 'US'),
Locale('da', 'DK'),
],
localizationsDelegates: [
AppLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
localeResolutionCallback: (locale, supportedLocales) {
if (locale == null) {
return supportedLocales.first;
}
for (var supportedLocales in supportedLocales ) {
if(supportedLocales.languageCode == locale.languageCode &&
supportedLocales.countryCode == locale.countryCode) {
return supportedLocales;
}
}
return supportedLocales.first;
},
home: Scaffold(
appBar: AppBar(
title: Text('Brew App'),
backgroundColor: Colors.green[800],
),
backgroundColor: Colors.green[100],
body: Container(
padding: EdgeInsets.all(30.0),
child: GridView.count(
crossAxisCount: 2,
children: <Widget>[
Card(
margin: EdgeInsets.all(8.0),
child: InkWell(
onTap: (){},
splashColor: Colors.green,
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Icon(Icons.add_box, size: 70.0, color: Colors.orange,),
Text("Calculator", style: TextStyle(fontSize: 17.0)),
],
),
),
)
),
Card(
margin: EdgeInsets.all(8.0),
child: InkWell(
onTap: (){},
splashColor: Colors.green,
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Icon(Icons.local_library, size: 70.0, color: Colors.blue,),
Text(
//"Knowledge",
AppLocalizations.of(context).translate('menu'),
style: TextStyle(fontSize: 17.0)),
],
),
),
)
),
],
),
),
),
);
}
}
app_localization
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class AppLocalizations {
final Locale locale;
AppLocalizations(this.locale);
static AppLocalizations of (BuildContext context) {
return Localizations.of<AppLocalizations>(context, AppLocalizations);
}
static const LocalizationsDelegate<AppLocalizations> delegate =
_AppLocalizationsDelegate();
Map<String, String> _localizedStrings;
Future<bool> load() async {
// Load the language JSON file from the "lang" folder
String jsonString =
await rootBundle.loadString('lang/${locale.languageCode}.json');
//print(locale.languageCode);
//await rootBundle.loadString('lang/${locale.languageCode}.yaml');
Map<String, dynamic> jsonMap = json.decode(jsonString);
_localizedStrings = jsonMap.map((key, value){
//print (_localizedStrings);
//print (locale);
return MapEntry(key, value.toString());
});
return true;
}
String translate(String key) {
return _localizedStrings[key];
}
}
//LocalizationsDelegate is a factory for a set of localized resources
class _AppLocalizationsDelegate
extends LocalizationsDelegate<AppLocalizations> {
const _AppLocalizationsDelegate();
@override
bool isSupported(Locale locale) {
//Include all of your supported language codes here
return ['en', 'da'].contains(locale.languageCode);
}
@override
Future<AppLocalizations> load(Locale locale) async {
AppLocalizations localizations = AppLocalizations(locale);
await localizations.load();
return localizations;
}
@override
bool shouldReload(_AppLocalizationsDelegate old) => false;
}