Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test localized widgets in flutter?

Tags:

flutter

I added l10n to my flutter app as suggested in the flutter docs, it is suggested to get the localizations with a static method like this:

static DemoLocalizations of(BuildContext context) {
  return Localizations.of<DemoLocalizations>(context, DemoLocalizations);
}

This works fine when running the app, but when I try to test my widgets, the returned value is always null.

Is there any easy way to provide the localizations within the tests?

For now I'm passing through the localizations via DI, but it is quite an overhead.

like image 498
svi3c Avatar asked Sep 23 '18 06:09

svi3c


3 Answers

You can wrap the widget you want to tests into a Localizations

Localizations(
  delegates: [
    yourDelegate
  ],
  locale: Locale('en'),
  child: YourWidget(),
);
like image 163
Rémi Rousselet Avatar answered Nov 16 '22 21:11

Rémi Rousselet


The above answer didn't help because I was calling strings from json file. The medium blog actually handled my problem in solving the issue.

like image 5
Hyo Avatar answered Nov 16 '22 20:11

Hyo


I think you should read this issues. In case file json over 10kb, it cannot load assets. We should keep following and wait Flutter team fix it or real solution https://github.com/flutter/flutter/issues/22193

like image 2
Quyen Anh Nguyen Avatar answered Nov 16 '22 21:11

Quyen Anh Nguyen