Aizan Sagheer
Aizan Sagheer

Reputation: 1454

AppLocalizations without context in flutter

I want to use

AppLocalization.of(context)!.home,

without context. I'm using this in my class model where there's no context. Here's my class:

class MenuItems {

 static const home = MenuItem('Home',Icons.home);
 static const frequentQuestion = MenuItem('Frequent Questions', Icons.question_answer);

 static const all = <MenuItem>[
      home,
      frequentQuestion,
 ];
}

Upvotes: 0

Views: 387

Answers (1)

Aizan Sagheer
Aizan Sagheer

Reputation: 1454

I have solved this issue by declaring variable global like

BuildContext? kAppcontext

Then I initialized in main.dart under build now wherever I need it I used it as

 static const frequentQuestion = MenuItem(AppLocalizations.of(kAppcontext!)!.home,Icons.home);

Upvotes: 1

Related Questions