1

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; } 
2
  • why not using this good plugin localize_and_translate Commented Jun 7, 2020 at 13:17
  • Have you tried adding a null checker on this line AppLocalizations.of(context).translate('menu'),? Like this AppLocalizations.of(context)!.translate('menu'), Commented Jul 30, 2021 at 1:38

1 Answer 1

1

You need to build the app first i.e. attempt to run the app. This should generate the localized Strings in {FLUTTER_PROJECT}/.dart_tool/flutter_gen/gen_l10n as mentioned in this guide. Without the generated file, the app won't be able to access those localized Strings.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.