Error trying to change the configured Locale using a Dart library

Asked

Viewed 198 times

-1

I’m trying to set up a new one locale on another screen of the application, but it says that the object I try to access is null, but I did exactly as the example, I’m not finding the error.

data.changeLocale(newLocale);

He makes the following mistake:

The following NoSuchMethodError was thrown while handling a gesture:
The method 'changeLocale' was called on null.
Receiver: null
Tried calling: changeLocale(Instance of 'Locale')

I can’t find the error, I did look like the official example of the library I’m using at Easylocation v 1.3.0 Example link :Link to github example The flutter doctor doesn’t detect any Issue, it’s all right with the environment. Any further information I may need will be on standby.

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:recase/recase.dart';
List languages =[];

Map<String,dynamic> locales = new Map();


Locale makeLocale(String localeString){

   if(localeString!= null) {
     Locale locale;
     String language = localeString;
     String languageCode;
     String countryCode;

     int firstLetterLangIndex = 0;
     int secondLetterLangIndex = 2;
     int firstLetterCountryIndex = 3;
     int secondLetterCountryIndex = 5;

     languageCode =
         language.substring(firstLetterLangIndex, secondLetterLangIndex);
     countryCode =
         language.substring(firstLetterCountryIndex, secondLetterCountryIndex);

     locale = Locale(languageCode, countryCode);
     return locale;
   }
   else{
     print("teste"+localeString);
     return null;
   }
}


void buildListLanguages(BuildContext context){
  languages =[];
  String configDataString = AppLocalizations.of(context).tr("config.otherLangs");
  List configStringList= configDataString.split(",");
  print(configStringList);

  int configStringIndex =0;
  do{
   if(configStringIndex % 2 ==0)
     languages.add(configStringList[configStringIndex]);
   else
     locales.addAll({configStringList[configStringIndex-1]:configStringList[configStringIndex]});
   configStringIndex++;

  }while(configStringIndex < configStringList.length);
}
class selecPage extends StatefulWidget {
  @override
  _selecPageState createState() => _selecPageState(
  ); }

class _selecPageState extends State<selecPage> {
  @override
  Widget build(BuildContext context) {
    buildListLanguages(context);
    TextStyle itemStyle =TextStyle(fontSize: 40,
    );
    ReCase rcTitle = new ReCase(AppLocalizations.of(context).tr('selectPage.desc'));
    var data = EasyLocalizationProvider.of(context).data;
    return EasyLocalizationProvider(
      data:data,
     child: CupertinoPageScaffold(
       navigationBar: CupertinoNavigationBar(
         middle: Text(rcTitle.sentenceCase),
       ),
       child:ListView.builder(
         itemCount: languages.length,
         itemBuilder: (context, index) {
           return GestureDetector(
             child:Card(child: Text(languages[index],textAlign: TextAlign.center,style: itemStyle,)),
             onTap: (){
               this.setState(() {
                 String tempLocaleString = locales[languages[index]];
                 tempLocaleString = tempLocaleString.replaceAll(" ","");
                 Locale newLocale = makeLocale(tempLocaleString);
                 data.changeLocale(newLocale);
                 data.saveLocale();
                 Navigator.pop(context);
               });

             },

           );
         },
       ) ,
     ),

    );



       ;
  }
}

/// fazer o recase de cada linguagem
/// construinfdo a lista de linguagens
/// e então usar a seleção para alterar  idioma
/// mandar mensagem toast para reiniciar se for necessário

1 answer

1


You’re probably not instantiating the EasyLocation.

In the Github documentation you showed us is told to instantiate the controller on main of your project, that way:

void main() => runApp(EasyLocalization(child: MyApp()));

The EasyLocationProvider search in your widget tree’s heritage by the instance created on the app startup.

If you have already instantiated, then I believe that point where you are trying to call the Provider is out of the main context, the point I say is the screen _selecPageState.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.