tr() of EasyLocalization is not accessible in flutter

I am trying to do language translation, I initialized it on main file,here my main file looks like

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await EasyLocalization.ensureInitialized();
  SystemChrome.setPreferredOrientations(
      [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]).then((_) {
    runApp(EasyLocalization(
        supportedLocales: [
          Locale('en'),
          Locale('tr'),
        ],
        path: 'assets/language',
        fallbackLocale: Locale('en'),
        child: BaseWidget()));
  });
}

BaseWidget file code

class BaseWidget extends StatefulWidget {
  const BaseWidget({Key? key}) : super(key: key);

  @override
  State<BaseWidget> createState() => _BaseWidgetState();
}

class _BaseWidgetState extends State<BaseWidget> {
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
        designSize: Size(360, 690),
        minTextAdapt: true,
        splitScreenMode: true,
        builder: (context, _) {
          return MaterialApp(
            localizationsDelegates: context.localizationDelegates,
            supportedLocales: context.supportedLocales,
            locale: context.locale,
            title: AppStrings.APP_TITLE_TEXT,
            debugShowCheckedModeBanner: false,
            onGenerateRoute: AppRouter().onGenerateRoute,
            initialRoute: AppRouteName.SPLASH_SCREEN_ROUTE,
            
          );
        });
  }
}

after splash screen I’m calling login screen and on it I’m doing something like this

class Login extends StatefulWidget {
  const Login({Key? key}) : super(key: key);

  @override
  State<Login> createState() => _LoginState();
}

class _LoginState extends State<Login> {
  bool isEn = true;
  @override
  Widget build(BuildContext context) {
    var media = MediaQuery.of(context);

    return BackgroundImage(
      child: Scaffold(
          backgroundColor: Colors.transparent,
          body: Column(
                Text(
                    'title'.tr() + ":",  //here tr() isn't accessible
                    style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                  ),

Please help how to do this.

Here are my languages file

en.json

{
    "title" : "Title",
    "demoText" : "This is a demo text!",
    "buttonText": "Toggle Language"
}

tr.json

{
    "title" : "Başlık",
    "demoText" : "Bu bir deneme yazısıdır!",
    "buttonText": "Dili Değiştir"
}

======================UPDATE=======================
if i do something like this to change the text langauge, it is not accessing locale methods

 CustomButton(
                        width: 0.75,
                        text: AppStrings.LOGIN_CAPITAL_TEXT,
                        onPressed: () {
                          setState(() {
                            isEn = !isEn;
                            isEn? context.setLocale(const Locale('en')) 
                            : context.setLocale(const Locale('tr'));
                          });
                        },
                      )

error context.setLocale(const Locale('en')) here

>Solution :

This error could happened because you are not import the package to your class:

import 'package:easy_localization/easy_localization.dart' 

Leave a Reply