skip to Main Content

I have a custom LocalizationDelegate that deals with my app localizations :

class LocalizationDelegate extends LocalizationsDelegate<Localization> {
  const LocalizationDelegate();

  @override
  bool isSupported(Locale locale) => ['en', 'fr'].contains(locale.languageCode);

  @override
  Future<Localization> load(Locale locale) async {
    String string = await rootBundle
        .loadString("assets/strings/${locale.languageCode}.json");
    language = json.decode(string);
    return SynchronousFuture<Localization>(Localization());
  }

  @override
  bool shouldReload(LocalizationDelegate old) => false;
}

And when i run this test :

testWidgets('Localization test', (WidgetTester tester) async {
    await tester.pumpWidget(const FakeTestPage());
    expect(find.text('TEST'), findsOneWidget);
    print("Done !");
  });

With FakeTestPage :

class FakeTestPage extends StatelessWidget {
  const FakeTestPage({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      localizationsDelegates: [
        LocalizationDelegate(),
        GlobalCupertinoLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
      ],
      supportedLocales: [Locale("fr"), Locale("en")],
      locale: Locale("fr"),
      home: Scaffold(
        body: Text("TEST"),
      ),
    );
  }
}

It throws the error :

/Users/foxtom/Desktop/flutter/bin/flutter --no-color test --machine --start-paused test/localization_test.dart
Testing started at 11:07 ...

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure was thrown running a test:
Expected: exactly one matching node in the widget tree
  Actual: _TextFinder:<zero widgets with text "TEST" (ignoring offstage widgets)>
   Which: means none were found but one was expected

When the exception was thrown, this was the stack:
#4      main.<anonymous closure> (file:///Users/foxtom/StudioProjects/TestProject/test/localization_test.dart:12:5)
<asynchronous suspension>
<asynchronous suspension>
(elided one frame from package:stack_trace)

This was caught by the test expectation on the following line:
  file:///Users/foxtom/StudioProjects/TestProject/test/localization_test.dart line 12
The test description was:
  Localization test
════════════════════════════════════════════════════════════════════════════════════════════════════

Test failed. See exception logs above.
The test description was: Localization test

And if i remove LocalizationDelegate() from the localizationsDelegates parameter in MaterialApp it’s working fine.

Is there something wrong with the Future aspect of the delegate ?
How can i make this working as it is in my real app ?

2

Answers


  1. Have a look at this flutter issue: https://github.com/flutter/flutter/issues/134999

    You are most likely loading a json file that is too large and your material app therefore doesn’t change to the new locale.

    Login or Signup to reply.
  2. You can try the following :

    await tester.pumpAndSettle();

    From this github issue.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search