Skip to content

Existing l10n.yaml file is mandatory or null check assertion is thrown in AppLocalizations.of #120457

@rjduffsep

Description

@rjduffsep

Steps to Reproduce

Following the documentation to install internationalization and localization except not adding the l10n.yaml file results in a null check operator exception when localizations are attempted to be used to set aMaterialApp.title property. Otherwise sensible defaults allow translations to work in the rest of the app. None of the default properties (arb-dir, template-arb-file, or output-localization-file) of the example l10n.yaml seem to have anything to do with using a null check operator versus returning a nullable type.

  1. Follow guide here to create a new project (flutter create test) and add internationalization.

  2. Follow guide here to add localizations, but do not add a l10n.yaml file (Step 3).

  3. Add app_en.arb localization file (Step 4) as described.

  4. Run flutter gen-l10n

  5. Modify main.dart as described by the guide.

  6. Call AppLocalizations.of in MyHomePage, see main.dart code file below.

  7. Run app and observe application runs with translated text.

  8. Call AppLocalizations.of in MaterialApp.title, see main.dart code file below.

  9. Run app and observe application hits an exception of "Null check operator used on a null value" in the of method of the AppLocalizations class:

static AppLocalizations of(BuildContext context) {
    return Localizations.of<AppLocalizations>(context, AppLocalizations)!;
}
  1. Add the l10n.yaml as described in step 3 of the localizations guide.
  2. Run flutter gen-l10n
  3. Observe that null check operator has disappeared from the app_localizations.dart file.
static AppLocalizations? of(BuildContext context) {
    return Localizations.of<AppLocalizations>(context, AppLocalizations);
}
  1. Run app and observe application runs without an exception.

Expected results:
Without a l10n.yaml file the AppLocalizations class file generated should still return a nullable type not throw a null check operator exception. Or a much stronger warning should be placed in the documented steps that broken behavior might occur without this file.

Actual results:
Application shows red screen with yellow text of "Null check operator used on a null value"
Debug window shows:

Exception has occurred.
_CastError (Null check operator used on a null value)
Code sample
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

void main() {
  runApp(const MyApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: AppLocalizations.of(context)?.helloWorld ?? '',  // BREAKS IF l10n.yaml FILE NOT PROVIDED
      localizationsDelegates: const [
        AppLocalizations.delegate,
        GlobalMaterialLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
      ],
      supportedLocales: const [
        Locale('en'), // English
        Locale('es'), // Spanish
      ],
      home: const MyHomePage(),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title:
            Text(AppLocalizations.of(context)?.helloWorld.toUpperCase() ?? ''),
      ),
      body: Center(
          child: Text(
              AppLocalizations.of(context)?.helloWorld.toLowerCase() ?? '')),
    );
  }
}

Logs
[  +21 ms]
           ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
           The following _CastError was thrown building MyApp(dirty):
           Null check operator used on a null value

           The relevant error-causing widget was:
             MyApp MyApp:file:///C:/Users/*********/Documents/workspace/test-flutter/test/lib/main.dart:6:16

           When the exception was thrown, this was the stack:
           #0      AppLocalizations.of (package:flutter_gen/gen_l10n/app_localizations.dart:67:73)
           #1      MyApp.build (package:test/main.dart:16:31)
           #2      StatelessElement.build (package:flutter/src/widgets/framework.dart:5038:49)
           #3      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4968:15)
           #4      Element.rebuild (package:flutter/src/widgets/framework.dart:4690:5)
           #5      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4950:5)
           #6      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4944:5)
           #7      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3953:16)
           #8      Element.updateChild (package:flutter/src/widgets/framework.dart:3682:18)
           #9      RenderObjectToWidgetElement._rebuild (package:flutter/src/widgets/binding.dart:1176:16)
           #10     RenderObjectToWidgetElement.mount (package:flutter/src/widgets/binding.dart:1145:5)
           #11     RenderObjectToWidgetAdapter.attachToRenderTree.<anonymous closure> (package:flutter/src/widgets/binding.dart:1092:18)
           #12     BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2682:19)
           #13     RenderObjectToWidgetAdapter.attachToRenderTree (package:flutter/src/widgets/binding.dart:1091:13)
           #14     WidgetsBinding.attachRootWidget (package:flutter/src/widgets/binding.dart:926:7)
           #15     WidgetsBinding.scheduleAttachRootWidget.<anonymous closure> (package:flutter/src/widgets/binding.dart:906:7)
           #19     _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:192:26)
           (elided 3 frames from class _Timer and dart:async-patch)

           ════════════════════════════════════════════════════════════════════════════════════════════════════
flutter analyze
Analyzing test...                                                       

warning - The receiver can't be null, so the null-aware operator '?.' is unnecessary - lib\main.dart:16:42 - invalid_null_aware_operator
warning - The receiver can't be null, so the null-aware operator '?.' is unnecessary - lib\main.dart:40:46 - invalid_null_aware_operator
warning - The receiver can't be null, so the null-aware operator '?.' is unnecessary - lib\main.dart:44:43 - invalid_null_aware_operator

3 issues found. (ran in 1.3s)
[√] Flutter (Channel stable, 3.7.1, on Microsoft Windows [Version
    10.0.22621.1105], locale en-US)
    • Flutter version 3.7.1 on channel stable at C:\src\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 7048ed95a5 (9 days ago), 2023-02-01 09:07:31
      -0800
    • Engine revision 800594f1f4
    • Dart version 2.19.1
    • DevTools version 2.20.1

[√] Windows Version (Installed version of Windows is version 10 or
    • Windows (desktop)            • windows       • windows-x64    • Microsoft Windows [Version 10.0.22621.1105]
    • Chrome (web)                 • chrome        • web-javascript • Google Chrome 109.0.5414.120
    • Edge (web)                   • edge          • web-javascript • Microsoft Edge 107.0.1418.62

[√] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!

Metadata

Metadata

Assignees

Labels

P1High-priority issues at the top of the work lista: internationalizationSupporting other languages or locales. (aka i18n)c: crashStack traces logged to the consoled: api docsIssues with https://api.flutter.dev/found in release: 3.7Found to occur in 3.7found in release: 3.8Found to occur in 3.8frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onr: fixedIssue is closed as already fixed in a newer version

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions