I am new in Flutter localization. I am trying to inplement it in my flutter IOS app but I am getting the below error:
The method 'translate' was called on null. Receiver: null Tried calling: translate("menu")
I hope someone can me with with this - I am been trying for hours but can't sole the issue - Hope you can help
Main:
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; }
AppLocalizations.of(context).translate('menu'),? Like thisAppLocalizations.of(context)!.translate('menu'),