Change flutter localizations with bloc state managment problem

Issue

I would like to change application language every time that someone decide to change application language without restarting app. Everything is working using BLoC.

The problem I have I don’t really understand one thing. If I pass to MaterialApp property title TodosLocalizations.of(context).translate("appTitle") it throws an error:

The method 'translate' was called on null.
Receiver: null
Tried calling: translate("appTitle")

enter image description here

But when I comment this line and pass the same thing to onGenerateTitle property using context everything is working without problem.

enter image description here

Can someone answer me why this happening or I might don’t understand how to use this property (title) in this case.

Solution

When you call onGenerateTitle: (BuildContext context) => TodosLocalizations.of(context).title, it uses a new BuildContext, which already contains the LocalizedDelagate(), so it can be called with TodosLocalizations.of(context).

When you use it directly without onGenerateTitle within the same build method, you refer to an instance of context before the LocalizedDelagate() was created, so TodosLocalizations.of(context) doesn’t return anything.

Answered By – Chinmay Mourya

Answer Checked By – Mildred Charles (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.