ilteoood/flutter_i18n

Inproper use of Null check operator

hkirk opened this issue · 6 comments

hkirk commented

Seems like checking forcedLocale isn't set.

E/flutter (12703): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Null check operator used on a null value
E/flutter (12703): #0 FlutterI18n.refresh (package:flutter_i18n/flutter_i18n.dart:87:74)

That's pretty strange: you have a null instance of FlutterI18n, which means that isn't loaded correctly.

Can you show me how did you configured it, or create a reproduction repo?

hkirk commented

Something like this - can try and make a reproduction repo, if nessesary.

Future<void> main() async {
  final FlutterI18nDelegate flutterI18nDelegate = FlutterI18nDelegate(
    translationLoader: FileTranslationLoader(
        useCountryCode: false,
        fallbackFile: 'en',
        basePath: 'assets/i18n'),
  );
  WidgetsFlutterBinding.ensureInitialized();
  await flutterI18nDelegate.load(Locale("en"));
  runApp(AppWidget(flutterI18nDelegate));
}


class AppWidget extends StatelessWidget {
  final FlutterI18nDelegate _flutterI18nDelegate;
  final AppRouter _router = AppRouter();

  AppWidget(this._flutterI18nDelegate);


  @override
  Widget build(BuildContext context) {
    Future.delayed(Duration.zero, () async {
      Locale? currentLocale = FlutterI18n.currentLocale(context);
      final currentLang = currentLocale == null ? "en" : currentLocale.languageCode;
      await FlutterI18n.refresh(context, Locale(currentLang));
    });
   return MultiBlocProvider(
      providers: [
        ...
      ],
      child: MaterialApp.router(
        title: "Test",
        localizationsDelegates: [
          _flutterI18nDelegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
        ],
        debugShowCheckedModeBanner: false,
        routerDelegate: _router.delegate(),
        routeInformationParser: _router.defaultRouteParser(),
      ),
    );
}

IMO is correct the error in this case.

The Future.delayed is executed before the _flutterI18nDelegate in the localizationsDelegates, so we don't have the i18n instance attached to the context.

IDK if that is for reproduction purpose, but the content of the delayed function is already made in the delegate

Catch the same issue with the Storybook package when integrate localization to storybook example.

════════ Exception caught by widgets library ═══════════════════════════════════
The following _CastError was thrown building Builder(dirty):
Null check operator used on a null value

The relevant error-causing widget was
Builder
package:flutter_template/main.dart:38
When the exception was thrown, this was the stack
#0      FlutterI18n.translate
#1      MyApp.build.<anonymous closure>
#2      Builder.build
#3      StatelessElement.build
#4      ComponentElement.performRebuild
...
════════════════════════════════════════════════════════════════════════════════

@buzzySmile

I've integrated it without any problem

image

Can you provide an example repository?

Closed for inactivity.